4

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
  • 2,788
  • 7
  • 30
  • 45
  • 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. http://api.jquery.com/ready/ Does it work in ie8? – Benjamin Gruenbaum Jun 09 '12 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 Jun 09 '12 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 Jun 09 '12 at 13:50
  • 1
    @caligula: That's a trick of determining IE. IE supports the comment before which contains a negating `!`. – pimvdb Jun 09 '12 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: http://msdn.microsoft.com/en-us/library/ie/cc197055(v=vs.85).aspx – Benjamin Gruenbaum Jun 09 '12 at 13:51
  • @caligula, this isn't my code, just thought it would solve the issue but it didn't. – CJT3 Jun 09 '12 at 13:51
  • @BenjaminGruenbaum, this still doesn't explain why it doesn't work when you first launch the browser, but **does** work if you reload the page or open it in a new tab or open the debug console. – CJT3 Jun 09 '12 at 13:53
  • Does it work when you load it, then clear the browser history and cache and reload it? If it doesn't then does it work using the newer addEventListener way? – Benjamin Gruenbaum Jun 09 '12 at 13:57
  • @BenjaminGruenbaum unfortunately, I do not have a way to test in IE8. I only have IE 9 on the computer I was borrowing to test compatibility. – CJT3 Jun 09 '12 at 14:03
  • @CharlesJohnThompsonIII no problem; btw, where is the code placed? ``, ``? – Ja͢ck Jun 09 '12 at 14:12
  • It's likely a timing issue. Where is this JavaScript 'inserted', in the '' of the document? If it's at the bottom of the page or in the body, I could see some disconnects. – JMC Jun 09 '12 at 14:12
  • @CharlesJohnThompsonIII oh, btw, trick to detect IE: http://ajaxian.com/archives/ievv – Ja͢ck Jun 09 '12 at 14:12
  • @JMC it is in the '', like it should be. – CJT3 Jun 09 '12 at 14:17

4 Answers4

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.

Ryan
  • 3,715
  • 2
  • 20
  • 17
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.

CJT3
  • 2,788
  • 7
  • 30
  • 45
Nickoli Roussakov
  • 3,434
  • 16
  • 22
  • 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. – CJT3 Jun 09 '12 at 13:55
  • 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? – Nickoli Roussakov Jun 09 '12 at 13:58
  • 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. – CJT3 Jun 09 '12 at 14:00
  • 1
    If this is the highest level, where is init coming from? – Wolfgang Stengel Jun 09 '12 at 14:10
  • @WolfgangStengel *init* is the function that I want to run when the document finishes loading. It is defined later in the javascript file. – CJT3 Jun 09 '12 at 14:19
  • Have you tried defining it before the onload wrapper? Although the function might be called later, the variable itself is being used immediately. – Wolfgang Stengel Jun 09 '12 at 14:23
  • @WolfgangStengel that makes sense, I will try this and let you know. – CJT3 Jun 09 '12 at 14:27
  • @WolfgangStengel nope, this makes no difference. – CJT3 Jun 09 '12 at 14:31
  • 1
    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. – Wolfgang Stengel Jun 09 '12 at 14:46
3

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.

Ahmed Hamdy
  • 2,547
  • 1
  • 17
  • 23
2

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');
        }
    }
}
CJT3
  • 2,788
  • 7
  • 30
  • 45