Why does JavaScript only work after opening developer tools in IE once?

前端 未结 12 2103
予麋鹿
予麋鹿 2020-11-22 02:20

IE9 Bug - JavaScript only works after opening developer tools once.

Our site offers free pdf downloads to users, and it has a simple \"enter password to download\" f

相关标签:
12条回答
  • 2020-11-22 02:29

    If you are using angular and ie 9, 10 or edge use :

    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';
    }]);
    

    To completely disable cache.

    0 讨论(0)
  • 2020-11-22 02:30

    Here's another possible reason besides the console.log issue (at least in IE11):

    When the console is not open, IE does pretty aggressive caching, so make sure that any $.ajax calls or XMLHttpRequest calls have caching set to false.

    For example:

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

    When the developer console is open, caching is less aggressive. Seems to be a bug (or maybe a feature?)

    0 讨论(0)
  • 2020-11-22 02:31

    HTML5 Boilerplate has a nice pre-made code for console problems fixing:

    // 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;
            }
        }
    }());
    

    As @plus- pointed in comments, latest version is available on their GitHub page

    0 讨论(0)
  • 2020-11-22 02:31

    If you are using AngularJS version 1.X you could use the $log service instead of using console.log directly.

    Simple service for logging. Default implementation safely writes the message into the browser's console (if present).

    https://docs.angularjs.org/api/ng/service/$log

    So if you have something similar to

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

    you can replace it with

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

    Angular 2+ does not have any built-in log service.

    0 讨论(0)
  • 2020-11-22 02:34

    I put the resolution and fix for my issue . Looks like AJAX request that I put inside my JavaScript was not processing because my page was having some cache problem. if your site or page has a caching problem you will not see that problem in developers/F12 mode. my cached JavaScript AJAX requests it may not work as expected and cause the execution to break which F12 has no problem at all. So just added new parameter to make cache false.

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

    Looks like IE specifically needs this to be false so that the AJAX and javascript activity run well.

    0 讨论(0)
  • 2020-11-22 02:35

    It sounds like you might have some debugging code in your javascript.

    The experience you're describing is typical of code which contain console.log() or any of the other console functionality.

    The console object is only activated when the Dev Toolbar is opened. Prior to that, calling the console object will result in it being reported as undefined. After the toolbar has been opened, the console will exist (even if the toolbar is subsequently closed), so your console calls will then work.

    There are a few solutions to this:

    The most obvious one is to go through your code removing references to console. You shouldn't be leaving stuff like that in production code anyway.

    If you want to keep the console references, you could wrap them in an if() statement, or some other conditional which checks whether the console object exists before trying to call it.

    0 讨论(0)
提交回复
热议问题