在我打开控制台之前,IE9脚本不会运行

IE9 script is not run until I open console

本文关键字:脚本 IE9 运行 控制台      更新时间:2023-09-26

我有一个包含JS代码的页面。当我在IE9中打开这个页面时,代码没有运行。然而,当我用F12打开控制台并重新加载页面时,脚本就会开始执行。这里可能出了什么问题?

检查状态栏上的JS错误图标。你可能会了解到console.log是如何不存在的,因为它直到你打开开发者工具(在IE中)才存在。

在代码中解决此问题的方法是检查console.log的存在,如果它不存在,请创建一个。

if (typeof console !== 'object' || typeof console.log !== 'function') {
    console.log = function (arg) {
        //now you can alert the argument or do whatever even when console.log isn't natively supported
    }
}