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

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

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

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

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

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

我如何使这工作在Internet Explorer?


当前回答

如果你使用的是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';
}]);

完全禁用缓存。

其他回答

如果你使用的是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';
}]);

完全禁用缓存。

对我来说,这发生在IE 11中。我在调用jquery。load函数。 所以我用了老方法,在url中放了一些东西来禁用缓存。

$("#divToReplaceHtml").load('@Url.Action("Action", "Controller")/' + @Model.ID + "?nocache=" + new Date().getTime());

我把解决方案和解决我的问题。看起来像AJAX请求,我把我的JavaScript没有处理,因为我的页面有一些缓存问题。如果你的站点或页面有缓存问题,在开发者/F12模式下你不会看到这个问题。我缓存的JavaScript AJAX请求,它可能不会像预期的那样工作,并导致执行中断,其中F12没有任何问题。 添加新参数使cache为false。

$.ajax({
  cache: false,
});

看起来IE特别需要这个为假,以便AJAX和javascript活动运行良好。

如果你使用的是AngularJS版本1。X你可以使用$log服务,而不是直接使用console.log。

简单的日志服务。默认实现将消息安全地写入浏览器的控制台(如果存在)。

https://docs.angularjs.org/api/ng/service/美元日志

如果你有类似的东西

angular.module('logExample', [])
  .controller('LogController', ['$scope', function($scope) {
    console.log('Hello World!');
 }]);

你可以用

angular.module('logExample', [])
  .controller('LogController', ['$scope', '$log', function($scope, $log) {
    $log.log('Hello World!');
 }]);

Angular 2+没有任何内置的日志服务。

除了console.log问题(至少在IE11中),还有另一个可能的原因:

当控制台没有打开时,IE会进行非常激进的缓存,所以要确保任何$。ajax调用或XMLHttpRequest调用缓存设置为false。

例如:

$.ajax({cache: false, ...})

当开发人员控制台打开时,缓存就不那么激进了。似乎是一个bug(或者可能是一个功能?)