[Solved-5 Solutions] Why does javascript only work after opening developer tools in IE once ? - JavaScript Tutorial



Problem:

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

Solution 1:

  • It sounds like we might have some debugging code in our 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 our console calls will then work.
  • There are a few Solutions to this:
    • The most obvious one is to go through our code removing references to console. We shouldn't be leaving stuff like that in production code anyway.
    • If we want to keep the console references, we could wrap them in an if() statement, or some other conditional which checks whether the console object exists before trying to call it.

Solution 2:

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

Solution 3:

  • 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 $.ajaxcalls 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.

Solution 4:

This solved my problem after we made a minor change to it. we added the following in my html page in order to fix the IE9 problem:

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

This solution does not work on IE 11 on Windows 7 64-bit.

Solution 5:

We can add this before any tag of javascript may be solve the problem,

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



Related Searches to javascript tutorial - Why does javascript only work after opening developer tools in IE once ?