admin管理员组

文章数量:1201992

For some reason, IE9 is not running my JavaScript code onload when the browser is launched for the first time that session. It seems to only run onload after the user refreshes the page. It will also run the JavaScript when the debug console is open.

How do I make it so the JavaScript runs onload after the browser is open? Is this just a bug of IE9?

I'll restate this so you understand: The code DOESN'T run if you go to the site after launching a new browser session. The code DOES run if you open the site in a new tab, or reload the page, or open the debug console

Here is the function I use to run my script onload (which works fine in NORMAL browsers):

(function (i) {
  var u = navigator.userAgent;
  var e = /*@cc_on!@*/
  false;
  var st = setTimeout;
  if (/webkit/i.test(u)) {
    st(function () {
      var dr = document.readyState;
      if (dr == "loaded" || dr == "complete") {
        i()
      } else {
        st(arguments.callee, 10);
      }
    }, 10);
  } else if ((/mozilla/i.test(u) && !/(compati)/.test(u)) || (/opera/i.test(u))) {
    document.addEventListener("DOMContentLoaded", i, false);
  } else if (e) {
    (function () {
      var t = document.createElement('doc:rdy');
      try {
        t.doScroll('left');
        i();
        t = null;
      } catch (e) {
        st(arguments.callee, 0);
      }
    })();
  } else {
    window.onload = i;
  }
})(init); //init is the function to call onload

For some reason, IE9 is not running my JavaScript code onload when the browser is launched for the first time that session. It seems to only run onload after the user refreshes the page. It will also run the JavaScript when the debug console is open.

How do I make it so the JavaScript runs onload after the browser is open? Is this just a bug of IE9?

I'll restate this so you understand: The code DOESN'T run if you go to the site after launching a new browser session. The code DOES run if you open the site in a new tab, or reload the page, or open the debug console

Here is the function I use to run my script onload (which works fine in NORMAL browsers):

(function (i) {
  var u = navigator.userAgent;
  var e = /*@cc_on!@*/
  false;
  var st = setTimeout;
  if (/webkit/i.test(u)) {
    st(function () {
      var dr = document.readyState;
      if (dr == "loaded" || dr == "complete") {
        i()
      } else {
        st(arguments.callee, 10);
      }
    }, 10);
  } else if ((/mozilla/i.test(u) && !/(compati)/.test(u)) || (/opera/i.test(u))) {
    document.addEventListener("DOMContentLoaded", i, false);
  } else if (e) {
    (function () {
      var t = document.createElement('doc:rdy');
      try {
        t.doScroll('left');
        i();
        t = null;
      } catch (e) {
        st(arguments.callee, 0);
      }
    })();
  } else {
    window.onload = i;
  }
})(init); //init is the function to call onload
Share Improve this question edited Jun 9, 2012 at 13:58 CJT3 asked Jun 9, 2012 at 13:25 CJT3CJT3 2,9087 gold badges33 silver badges45 bronze badges 13
  • 8 As a good javascript tip, always use feature detection and not browser detection. It hurts my eyes to see this code. You can solve most cross-browser issues you experience by using jQuery. api.jquery.com/ready Does it work in ie8? – Benjamin Gruenbaum Commented Jun 9, 2012 at 13:28
  • 2 I refuse to use jQuery. I'll stick to hand coding thank you. I'm interested in actually using JavaScript, not a library. This code really isn't the issue, also I found it somewhere thinking it would solve my issue (which it didn't). The main point is that it works fine when you reload the page or open the debug console, but not when you first launch the browser and go to the site - which makes no sense! – CJT3 Commented Jun 9, 2012 at 13:41
  • Can you comment your code? I can't understand what you are doing. You assign a false to the 'e' variable and then use it in "else if (e)". What is the sense of this? – d.k Commented Jun 9, 2012 at 13:50
  • 1 @caligula: That's a trick of determining IE. IE supports the comment before which contains a negating !. – pimvdb Commented Jun 9, 2012 at 13:50
  • 2 If you absolutely insist on not using jQuery then window.onload should not be used in IE9 anyway but rather you should use window.addEventListener( "load", doLoad, false ); See this: msdn.microsoft.com/en-us/library/ie/cc197055(v=vs.85).aspx – Benjamin Gruenbaum Commented Jun 9, 2012 at 13:51
 |  Show 8 more comments

4 Answers 4

Reset to default 14

I had the exact same issue that you had. I had a set of images that I wanted to ensure were preloaded before I began starting a slideshow. I was making use of

$(window).load(function(){
    //All my code
});

And this is exactly what I was facing.

  • When I copied and pasted the URL in IE, the onload event did not seem to fire.
  • If I open the console using F12 and then past the URL in the browser and pressed enter, the everything seemed to be working.
  • Now that I opened the console at least once,
    • If I closeed the console and then reloaded the page, the onload was firing.
    • If I typed the URL and then pressed enter, the onload was firing.

It took me a couple of days to actually figure out what I was doing wrong.

The issue was with the console.log statements. At a lot of places in my code, I had done a lot of console logging. Even one of the plugins that I was using - jplayer has a an uncommented console message somewhere in the code.

The issue was that, unless you open the console at least once in IE, the console object is not available. Which means that the code will fail at the first console.log that it encounters.

Now, I was in no mood to comment out all my console.log statements just for the sake of testing it in IE. So, this is what I did instead. Right at the top of my document.ready jquery function, I wrote this small snippet of code.

if(!window.console){
    console={};
    console.log = function(){};
}

What it basically does is creates a dummy console.log function placeholder so that the code can run in IE but it will work only as long as console.log is the only console function that you are making use of in your code or in your plugins.

Just my 2 cents. Been pulling my hair over this issue for longer than I care to admit. I hope this is useful to at least someone.

You need to figure out if the code doesn't run at all, I.e. never enters your function, or if it fails on some specific line inside your function. Does IE9 show any warnings or js errors?

The easiest thing to do is stick a bunch of alert() statements in the code to see where it stops and narrow down to that line.

If it never enters your function then you need to look higher, where the call is being made.

Just a small note; When you use any debugging keywords (like console.log) or anything related, IE9 will escape this JS function if and only if the debugger is not on (with F12)

Actually I don't know what else cause a problem, but for me, my problem was the word "console.log" while debugger not on in IE9 ... I know this is already an answered question, but I felt it needs to be be known.

Okay, I figured it out. It has to do with some weird way IE handles IF statements.

In my init function I had two IF statements, one which checked if a variable existed and then logged the value of that variable. The other which checked to see if the value of the same variable was equal to an arbitrary string.

After removing the first IF statement, everything seems to work properly. I also decided to use a different onload function which can be seen below:

if (document.addEventListener) {
    document.addEventListener('DOMContentLoaded', init, true);
} else if (document.all && !window.opera){ //Crude test for IE
//Define a "blank" external JavaScript tag
    document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>');
    var contentloadtag=document.getElementById("contentloadtag");
    contentloadtag.onreadystatechange=function(){
        if (this.readyState=="complete") {
            init();
            //ie('open');
        }
    }
}

本文标签: internet explorer 9IE9 not running javascript onloadStack Overflow