Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 :)

like image 396
Manohar Avatar asked May 20 '09 07:05

Manohar


People also ask

Does document onload and window onload fire at the same time?

The general idea is that window. onload fires when the document's window is ready for presentation and document. onload fires when the DOM tree (built from the markup code within the document) is completed.

Can I use window onload?

window. onload just runs when the browser gets to it. window. addEventListener waits for the window to be loaded before running it.

Can I use onload on Div?

The onload event can only be used on the document(body) itself, frames, images, and scripts. In other words, it can be attached to only body and/or each external resource. The div is not an external resource and it's loaded as part of the body, so the onload event doesn't apply there.

What is window onload init?

window. onload = init(); assigns the onload event to whatever is returned from the init function when it's executed. init will be executed immediately, (like, now, not when the window is done loading) and the result will be assigned to window. onload .


2 Answers

For IE try:

window.onload = new function() { alert('hello');}; 
like image 172
George Avatar answered Nov 10 '22 06:11

George


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.

like image 27
Milan Adamovsky Avatar answered Nov 10 '22 06:11

Milan Adamovsky