在激活开发人员工具之前,使用JS的网站在IE9中不起作用 [英] Website with JS doesn't work in IE9 until the Developer Tools is activated

查看:161
本文介绍了在激活开发人员工具之前,使用JS的网站在IE9中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个复杂的网站,它充分利用了jQuery和许多脚本。在加载网站时,我的脚本都没有工作(虽然我可以确认其他脚本运行正常)。除了一件事,我不会在SE上发布这样一个蹩脚的问题:

I'm developing a complex website that heavily leverages jQuery and a number of scripts. On load of the site, none of my scripting is working (though I can confirm that other scripts are functioning fine). I wouldn't be posting such a lame question here on SE except for one thing:

我点击F12打开开发人员工具,这样我就可以调试我的问题,一切都立即完美!

The instant I hit F12 to turn on developer tools so I can debug my issue, everything instantly works perfectly!

更糟糕的是,如果我关闭浏览器,启动它,首先打开Dev Tools并访问该网站,一切都按预期工作。

Worse, if I shut down the browser, start it up, turn on Dev Tools first and visit the site, everything works as expected.

所以我甚至无法调试这个问题因为Dev Tools修复了它! Dev Tools可以做些什么让事情发挥作用?它是否改变了UA(我做了一些jQuery.browser检测)?它是否对doctype有效?

So I can't even debug the darned problem because Dev Tools fixes it! What could Dev Tools be doing that makes things work? Does it change the UA (I do some jQuery.browser detection)? Does it do something to doctype?

编辑

我的所有控制台日志记录都包含在以下包装器实用程序函数中:

All my console logging is wrapped in the following wrapper utility function:

   function log(msg){
    if (console){
        console.log(msg);
    }
   }

我可以尝试任何想法或建议。如果我找到解决方案,我会在这里发帖。

Any thoughts or suggestions I could try would be welcome. I'll post here if I find a solution.

推荐答案

我很欣赏我在这里参加派对的时间已经很晚了,但是我有一个IE9的解决方案有点不同。

I appreciate I'm pretty late to the party here, but I've got a solution for IE9 that's a little different.

(function() {
    var temp_log = [];
    function log() {
        if (console && console.log) {
            for (var i = 0; i < temp_log.length; i++) {
                console.log.call(window, temp_log[i]);
            }
            console.log.call(window, arguments);
        } else {
            temp_log.push(arguments);
        }
    }
})();

您使用的基本上不是 console.log 日志。如果 console.log 存在,则它正常工作,否则它将日志条目存储在一个数组中,并在下一个日志 控制台可用的地方。

Basically instead of console.log you use log. If console.log exists then it works as normal, otherwise it stores log entries in an array and outputs them on the next log where the console is available.

如果它一推出数据就会很好code> console 可用,但这比设置自定义setInterval监听器便宜。

It would be nice if it pushed the data as soon as the console is available, but this is less expensive than setting up a custom setInterval listener.

我已经更新了这个脚本供我自己使用,我想我会分享它。它有一些值得改进:

I've updated this script for my own use and thought I'd share it. It has a few worthy improvements:


  • 使用 console.log()就像正常一样,即不再需要使用非标准 log()

  • 支持多个参数,例如 console.log('foo','bar')

  • 你也可以使用 console.error console.warn console.info (尽管将它们输出为 console.log

  • 脚本每1000毫秒检查本机控制台并在找到时输出缓冲区

  • use console.log() like normal, i.e. no longer need to use non-standard log()
  • supports multiple arguments, e.g. console.log('foo', 'bar')
  • you can also use console.error, console.warn and console.info (though outputs them as console.log)
  • script checks for native console every 1000ms and outputs the buffer when found

我认为通过这些改进,这已成为IE9的一个非常可靠的垫片。 在此查看GitHub回购

I think with these improvements, this has become a pretty solid shim for IE9. Check out the GitHub repo here.

if (!window.console) (function() {

    var __console, Console;

    Console = function() {
        var check = setInterval(function() {
            var f;
            if (window.console && console.log && !console.__buffer) {
                clearInterval(check);
                f = (Function.prototype.bind) ? Function.prototype.bind.call(console.log, console) : console.log;
                for (var i = 0; i < __console.__buffer.length; i++) f.apply(console, __console.__buffer[i]);
            }
        }, 1000); 

        function log() {
            this.__buffer.push(arguments);
        }

        this.log = log;
        this.error = log;
        this.warn = log;
        this.info = log;
        this.__buffer = [];
    };

    __console = window.console = new Console();
})();

这篇关于在激活开发人员工具之前,使用JS的网站在IE9中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆