gpt4 book ai didi

javascript - 避免 IE 抛出 : Error: 'console' is undefined 的常见习语

转载 作者:数据小太阳 更新时间:2023-10-29 05:06:48 25 4
gpt4 key购买 nike

我已经安装了 firebug 并编写了所有这些日志语句。

我已经在 IE 中测试了我的应用程序,当然我遇到了“未定义”错误。

避免这种情况的常用习语是什么。

我真的不想评论我文件中的所有 console.log 语句,也不想 mock 它们。

我不知道该怎么做。

最佳答案

我通常会像这样制作一个包装函数:

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

或者您可以在脚本文件/元素的开头执行类似的操作:

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

关于javascript - 避免 IE 抛出 : Error: 'console' is undefined 的常见习语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1146309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com