0

I am using jquery autosuggest to populate the data in a text box. When i am trying to work that functionality in ie8 i am getting the error: console not define

The jquery code where it is showing error is :

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

Please help me in solving the error

antyrat
  • 27,479
  • 9
  • 75
  • 76
ramsai
  • 121
  • 1
  • 7
  • 22

2 Answers2

2

Contrary to popular belief, the console exists in IE too. However console is not defined until after opening the Developer Tools (press F12). So unless the Developer Tools are already open when the page is loaded, then it will fail.

One solution is to add something like the following to the top of your file (i.e. before you use console):

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

This ensures that console.log is always available, even if it is a no-op.

Supr
  • 18,572
  • 3
  • 31
  • 36
  • Thank you very much for all my friends who helped me to get out of that error and now i am working fine with it. Thanks a lot to stackoverflow for this wonderful application which helps lot of guys like me – ramsai Jun 28 '12 at 11:40
1
if(window.console && window.console.log)
     console.log(data.length)
else
    alert(data.length);    
Johan
  • 35,120
  • 54
  • 178
  • 293