Why does JavaScript only work after opening developer tools in IE once?
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.
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