A JavaScript script only works on Internet Explorer when the Internet Explorer Developer Toolbar is visible
Asked Answered
L

3

19

I got a script working on Firefox 5 but not with Internet Explorer 9. When I just open the Internet Explorer Developer Toolbar addon and try the same actions as before - it works. There is other JavaScript code on the page which is working, so it is just a part that isn't.

Is there something like the developer toolbar changing any options of Internet Explorer while running?

Lehman answered 31/7, 2011 at 12:10 Comment(2)
@T.J.Crowder It seems the odds were low :-) ?Superintendency
As everyone already mentioned the problem is that the console object doesn't exists in some instances/browsers. Try this console wrapper to avoid the errors, it gives you a cross-browser console logging solution - benalman.com/projects/javascript-debug-console-logEtra
G
53

Without your having quoted any code, one has to guess.

My guess is that you're using console.log (or one of the other console methods) in your code. On IE8 and IE9, the console object doesn't exist until/unless the developer tools are open. Strange but true.

You should be getting script errors along the lines of "console is undefined" when you don't have the dev tools open.

Because of this, and because console doesn't exist in every browser (certainly not IE6 or IE7, which still combined make up about 18% of the general browsing users), it's best not to include them in production code or to check proactively that console exists before using it.

Gader answered 31/7, 2011 at 12:12 Comment(5)
Thank you for saving me (insert_too_much_time_here) hours, @T.J. Crowder! Your answer was the first (and last) in my search for what could be causing me this issue... Note to self: check for console before using!Adulterant
Thanks for this... really saved me. Funny how even msdn couldn't answer this question.Nucleus
Man, I can't give you enough upvotes... Thanks a million! - Guys, remember to upvote also the question :PReiko
THANK YOU! I could simply not understand what was causing this error! And why do Internet Explorer not throw any errors! Such a bitch!Veronicaveronika
If I found aladdin's magic lamp one day, I'll ask for removing IE from history.Argali
I
7

Is your script accessing or running any methods that are only available when the developer toolbar is open, such as console.log? For example, running console.log when console is undefined because the developer toolbar isn't open will cause an exception to be thrown.

Intreat answered 31/7, 2011 at 12:12 Comment(1)
Thanks, it was console.log causing my problems!Lehman
G
0

As mentioned in a similar question, use this code (in a script tag at the top of your page before other script tags, preferably):

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

or find a more up to date version of this same code here: https://github.com/h5bp/html5-boilerplate/blob/master/src/js/plugins.js

This just solved that same issue for me.

Genitourinary answered 5/2, 2016 at 17:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.