如何纠正这个错误,即8?

时间:2012-06-27 12:04:30

标签: jquery internet-explorer-8

我正在使用jquery autosuggest来填充文本框中的数据。当我试图在ie8中运行该功能时,我收到错误:console not defined

显示错误的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

请帮我解决错误

2 个答案:

答案 0 :(得分:2)

与流行的看法相反,控制台也存在于IE中。但是,在打开开发人员工具(按F12)之后才定义console。因此,除非在加载页面时开发人员工具已经打开,否则它将失败。

一种解决方案是在文件顶部添加类似以下内容(即在使用控制台之前):

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

这确保console.log始终可用,即使它是无操作。

答案 1 :(得分:1)

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