IE9 Bug - JavaScript只能在打开一次开发工具后才能工作。

我们的网站为用户提供免费的pdf下载,它有一个简单的“输入密码下载”功能。但是,它在ie浏览器中完全不起作用。

你可以在这个例子中看到。

下载通道是“makeuseof”。在任何其他浏览器中,它都可以正常工作。在IE中,两个按钮什么都不做。

我发现最奇怪的事情是,如果你用F12打开和关闭开发人员工具栏,它就会突然开始工作。

我们已经尝试了兼容模式等,没有什么不同。

我如何使这工作在Internet Explorer?


当前回答

HTML5 Boilerplate有一个很好的预置代码,用于修复主机问题:

// Avoid `console` errors in browsers that lack a console.
(function() {
    var method;
    var noop = function () {};
    var methods = [
        'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error',
        'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log',
        'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd',
        'timeStamp', 'trace', 'warn'
    ];
    var length = methods.length;
    var console = (window.console = window.console || {});

    while (length--) {
        method = methods[length];

        // Only stub undefined methods.
        if (!console[method]) {
            console[method] = noop;
        }
    }
}());

正如@加号在评论中指出的,最新版本在他们的GitHub页面上可用

其他回答

如果你使用的是angular和ie 9,10或edge:

myModule.config(['$httpProvider', function($httpProvider) {
    //initialize get if not there
    if (!$httpProvider.defaults.headers.get) {
        $httpProvider.defaults.headers.get = {};    
    }    

    // Answer edited to include suggestions from comments
    // because previous version of code introduced browser-related errors

    //disable IE ajax request caching
    $httpProvider.defaults.headers.get['If-Modified-Since'] = 'Mon, 26 Jul 1997 05:00:00 GMT';
    // extra
    $httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
    $httpProvider.defaults.headers.get['Pragma'] = 'no-cache';
}]);

完全禁用缓存。

我想这可能会有帮助,在任何javascript标签之前添加这个:

try{
  console
}catch(e){
   console={}; console.log = function(){};
}

这解决了我的问题后,我做了一个小的改变。为了解决IE9的问题,我在我的html页面中添加了以下内容:

<script type="text/javascript">
    // IE9 fix
    if(!window.console) {
        var console = {
            log : function(){},
            warn : function(){},
            error : function(){},
            time : function(){},
            timeEnd : function(){}
        }
    }
</script>

对于runeks和todotresde提供的解决方案,我还有另一种选择,它也避免了Spudley回答的评论中讨论的陷阱:

        try {
            console.log(message);
        } catch (e) {
        }

它有点邋遢,但另一方面,它简洁明了,涵盖了runeks回答中涵盖的所有日志记录方法,它有一个巨大的优势,你可以在任何时候打开IE的控制台窗口,日志就会源源不断地涌进来。

除了在公认答案中提到的“控制台”使用问题之外,至少还有另一个原因,为什么有时Internet Explorer中的页面只能在激活开发人员工具时才能工作。

当开发者工具被启用时,IE不会像正常模式那样真正使用HTTP缓存(至少在ie11中是默认的)。

这意味着如果你的网站或页面有缓存问题(如果它缓存的比它应该的多,例如-这是我的情况),你在F12模式下不会看到这个问题。因此,如果javascript执行一些缓存的AJAX请求,它们可能无法在正常模式下正常工作,而在F12模式下正常工作。