2012-06-27 36 views
0

我正在使用jquery autosuggest來填充文本框中的數據。當我努力工作,在IE8這個功能我得到的錯誤:控制檯沒有定義如何在ie 8中糾正這個錯誤?

在那裏示值誤差jQuery代碼是:

function lookup(inputString) { 
     if(inputString.length == 0) { 
      // Hide the suggestion box. 
      $('#suggestions').hide(); 
     } else { 
      // post data to our php processing page and if there is a return greater than zero 
      // show the suggestions box 
      $.post("string_search.php", {mysearchString: ""+inputString+""}, function(data){ 

       **console.log(data.length)** 
       if(data.length >0) { 
        $('#suggestions').show(); 
        $('#autoSuggestionsList').html(data); 
       }else{ 
       $('#suggestions').hide(); 
       } 
      }); 
     } 
    } //end 

請幫我解決錯誤

+0

控制檯在IE中不存在 – voigtan

+1

這篇文章有一個答案和一個備用策略。 http://stackoverflow.com/questions/690251/what-happened-to-console-log-in-ie8 – chapman84

+0

@voigtan它不??哇,原因不使用IE只是繼續上升... – Alnitak

回答

2

與流行的觀點相反,控制檯也存在於IE中。然而,console只有在打開開發工具後才能定義(按F12)。因此,除非加載頁面時開發者工具已經打開,否則它將失敗。

一種解決方案是(你使用控制檯前,IE)添加類似以下內容到你的文件的頂部:

<script> 
    try { 
     console.log('Hello console!'); 
    } catch(e) { 
     console = {log: function(){}}; 
    } 
</script> 

這確保了console.log始終可用,即使它是一個空操作。

+0

非常感謝所有幫助我擺脫這個錯誤的朋友,現在我的工作很好。非常感謝stackoverflow爲這個美妙的應用程序,這有助於很多像我一樣的人 – ramsai

1
if(window.console && window.console.log) 
    console.log(data.length) 
else 
    alert(data.length);