Common idiom to avoid IE throw: Error: 'console' is undefined

烂漫一生 提交于 2019-11-27 03:32:33

问题


I've installed firebug and I wrote all these log statements.

I've tested my app in IE and of course I've got "undefined" error.

What's the common idiom to avoid this.

I don't really feel like commenting all the console.log statements in my file nor to mock them.

Well I'm not sure what to do.


回答1:


i usually make a wrapper function like so:

function log(obj) {
    if (window.console && console.log) console.log(obj);
}

or you could do something like this at the beginning of your script file/element:

if (!window.console) { 
    window.console = {
        log: function(obj){ /* define own logging function here, or leave empty */ }
    };
}



回答2:


Paul Irish has a better wrapper for console.log().

http://paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/

This allows multiple arguments, and provides a history (for debugging) in case no console is there or (eg Firebug Lite) the console is created later.



来源:https://stackoverflow.com/questions/1146309/common-idiom-to-avoid-ie-throw-error-console-is-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!