IE9 not running javascript onload

19,722

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 4

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.

Share:
19,722
CJT3
Author by

CJT3

CEO of Portland based production company Wolf &amp; Thunder Productions. I've been writing code since I was around 12 years old. Starting with ActionScript, then moving on to HTML, CSS, Javascript, PHP, and other web languages. I tend to prefer to use my own scripts to program, as opposed to using a library, because I enjoy learning all aspects of development, and think it is important to know exactly what the code you're using is doing. It definitely makes it cool when I know how my websites operate and where potential stability issues might be so that I may troubleshoot them easier.

Updated on June 04, 2022

Comments

  • CJT3
    CJT3 almost 2 years

    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
    
  • CJT3
    CJT3 almost 12 years
    Apparently nobody is listening to me. 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.
  • Nickoli Roussakov
    Nickoli Roussakov almost 12 years
    If the code doesn't run, then it's the way you call it that must be the culprit, not the snippet that you've pasted. Can you show the code that calls this function?
  • CJT3
    CJT3 almost 12 years
    there is no code that calls this, this runs when the javascript is loaded and then determines when the document is loaded to call my init function. This is as high as you can go.
  • Wolfgang Stengel
    Wolfgang Stengel almost 12 years
    If this is the highest level, where is init coming from?
  • CJT3
    CJT3 almost 12 years
    @WolfgangStengel init is the function that I want to run when the document finishes loading. It is defined later in the javascript file.
  • Wolfgang Stengel
    Wolfgang Stengel almost 12 years
    Have you tried defining it before the onload wrapper? Although the function might be called later, the variable itself is being used immediately.
  • CJT3
    CJT3 almost 12 years
    @WolfgangStengel that makes sense, I will try this and let you know.
  • CJT3
    CJT3 almost 12 years
    @WolfgangStengel nope, this makes no difference.
  • Wolfgang Stengel
    Wolfgang Stengel almost 12 years
    Too bad. Can you send the complete setup? Does only the init function not run or does the execution not even reach the inner workings of the onload wrapper? I can't see why this should sometimes work and sometimes not, if everything's executed linearly.
  • railsy
    railsy over 10 years
    thanks for that, console.log was breaking my code in IE, commenting it out fixed it
  • Lukas Eder
    Lukas Eder over 10 years
    While this works for IE 9, I'd really prefer using jQuery(contentloadtag).ready(init); It's just more readable and robust.
  • CJT3
    CJT3 over 10 years
    But I don't use jQuery.
  • semiomant
    semiomant almost 7 years
    as one still cursed with maintaining for IE9, I thank you for finding this