Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE9 not running javascript 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
like image 622
CJT3 Avatar asked Nov 29 '22 02:11

CJT3


1 Answers

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.

like image 181
Ryan Avatar answered Dec 16 '22 00:12

Ryan