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 :)
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.
window. onload just runs when the browser gets to it. window. addEventListener waits for the window to be loaded before running it.
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.
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 .
For IE try:
window.onload = new function() { alert('hello');};
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With