window.onload() is not firing with IE 8 in first shot

77,564

Solution 1

For IE try:

window.onload = new function() { alert('hello');};

Solution 2

This is a pretty old thread but I found a solution that might help others.

I was adding a function to window.onload via a dynamically injected script (really to mimic an external script load for reasons which are not important in this context). As stated in this post, on the first load within IE8, the window.onload would not fire, but all subsequent calls would.

I found out that if I put this in the HTML file as an internal script, it would work everytime:

var windowOnload = window.onload || function() {};
window.onload = function() { windowOnload(); };

All the above code does is "initializes" IE8's window.onload unobtrusively. I suspect that IE8 fails to trigger window.onload the first time if it is called from an external script as the onload event isn't attached yet to window (or in tech terms, its typeof is undefined). It seems that the first time that's what IE8 is doing: attaching onload to window without executing it properly.

The above code then becomes quite obvious: We are merely forcing IE8 to recognize the onload event. We don't care what gets executed, or what doesn't, so we simply make sure to pipe on through any existing window.onload code that is already defined (just as a precaution).

It is important to have this as an internal script to the HTML (at least from my testing).

Your HTML would thus look something like this (the relevant parts):

 <script type="text/javascript">
  var windowOnload=window.onload||function(){};window.onload=function(){windowOnload();};
 </script>
 <script type="text/javascript" src="script.js">
 </script>

From my testing, after clearing cache, and reloading the page, I have gotten window.onload to successfully trigger each time.

I hope this helps.

Solution 3

You could have an error in your JavaScript's, if that happens, any JavaScript after that will not function correctly.

Try to remove the reference to login.js and common.js and try an alert within your problematic function.

Solution 4

I don't have IE8 to personally test, but what does this test do?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Test IE 8</title>
<script type="text/javascript">
/* <![CDATA[ */
window.onload = function(){alert('Good morning!');}
/* ]]> */
</script>
</head>
<body>
<h1>Hello</h1>
<body>
</html>

If this test works as expected, try the CDATA bit around your internal JavaScript block.

And then, if that does not work as expected, there is probably something in the external JavaScript above it that prevents your onload from firing. The previous poster mentioned this. At that point, try your error console or debugger to point the way.

Solution 5

onload fires after ALL your content has loaded (including external images etc). It's possible those resources are taking a long time to load on the first go (before they are cached). Another possibility is an error in your code that only affects IE as is stopping your scripts (but only the first time is odd).

Share:
77,564
Manohar
Author by

Manohar

I am here to learn and share knowledge and enlighten myself and others from ignorance.

Updated on October 29, 2020

Comments

  • Manohar
    Manohar over 3 years

    I am trying to make my pages work correctly with IE 8, I found out from here: http://www.masykur.web.id/post/How-to-Make-Our-Website-to-be-Ready-for-IE8.aspx that, my page has to be XHTML 1.0 compliant and atleast CSS 2.1 compliant, I made my page and CSS compliant with only few warnings, but still window.onload() is not firing. Does anybody encountered this problem?

    here is the code snippet:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
        <head>
            <meta http-equiv="X-UA-Compatible" content="IE=8"/>
            <title>Testing</title>
            <link rel="stylesheet" href="test.css" type="text/css"></link>
            <script type="text/javascript" src="login.js"></script>
            <script type="text/javascript" src="common.js"></script>
            <script type="text/javascript">
                window.onload = function()
                {
                              // Not coming here at all on first shot   
                }
            </script>
        </head>
        <body>
         .
         .
         .
    

    However refreshing the page seems to make it work.

    Am I missing something here?

    UPDATE:

    One of the IE addons created this problem, after disabling its working fine. Thanks for your time and answers :)