jamesmhaley
jamesmhaley

Reputation: 45459

JavaScript not running in IE at first run

Having a rather strange issue within IE. When I launch IE and load a page (from a local domain, let's call it "http://amazing.dev/") the JavaScript on the site does not run.

I know what you are thinking, turn on JS you plonker (http://goo.gl/FnzoW). It's on and the reason I know it's on is that when I launch developer tools and reload the page, the JS runs.

There is a lot of JS within this site, so it could be anything. But there are no errors, no warnings, nothing. On reload, works perfectly!

Any ideas/experience of this would help massively! It could just be my machine!

Upvotes: 0

Views: 100

Answers (2)

shaish
shaish

Reputation: 1499

The problem is that on IE there isn't a console object when the debugger is not open. that's why with the debugger open everything works fine for you.

just add this to the beginning of the page and it will probably work.

if (typeof console == "undefined") {
    this.console = {log: function() {}};
}

Upvotes: 1

Denys Séguret
Denys Séguret

Reputation: 382150

If the problem occurs only when you don't launch the developer tools, there is probably a call to console.log somewhere in your code. The console isn't available until you open those tools.

You can use this code (I'm not the author) to prevent the crash :

if (!window.console) {
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
    "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
    window.console = {};
    for (var i = 0; i < names.length; ++i) {
        window.console[names[i]] = function() {};
    }
};

Upvotes: 2

Related Questions