Closed Bug 159285 Opened 23 years ago Closed 23 years ago

document.onload firing before document loaded

Categories

(Core :: DOM: Events, defect)

x86
Windows 98
defect
Not set
minor

Tracking

()

VERIFIED INVALID

People

(Reporter: will, Assigned: joki)

References

()

Details

From Bugzilla Helper: User-Agent: Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.1b) Gecko/20020721 BuildID: 2002072104 in a page containing javascript which defines document.onload = function() the call to function is happening before the document is loaded in particular a call to document.getElementById(name) will fail when called whereas it doesn't when called from the body onload="" attribute Reproducible: Always Steps to Reproduce: 1.Load the page http://odin.himinbi.org/~will/test_document_load.html 2.Note "no" at the end of first alert message 3.Note "yes" at the end of second alert message Actual Results: Two alerts come up; the first (fired by document.onload) representing that document.getElementById failed and the second (fired by body onload="") representing that the same call succeeded Expected Results: Both alerts should succeed. As best I can tell the onload event should not be fired until the document is loaded. http://www.w3c.org/TR/2000/REC-DOM-Level-2-Events-20001113/events.html#Events-eventgroupings-htmlevents
It seems that the browser fires document.onload when it finishes download the document source and /then/ the browser executes all other scripts: <html> <script type="text/javascript"> document.onload = alert('document.onload fired'); </script> <body onload="alert('body.onload fired')"> <script type="text/javascript"> alert('embeded script executed'); </script> </body> </html> you will see three messages in this order: document.onload fired, embed script executed, body.onload fired the result is the same with MSIE5 so I guess the expected behavior is that (HTML)Document is 'loaded' before it fully inserts all the (HTML)Elements.
document.onload = init("document.onload") means "Run the function 'init' right now, passing it the argument 'document.onload'. The return value will be a function which should be installed as the onload handler on the document". So the behavior on your testcase is in fact correct. What you want to do is: document.onload = init; (note that 'init' is _not_ called in this case; it is merely assigned to the onload handler. It will be called with a single argument which is the onload event when the load actually completes).
Status: UNCONFIRMED → RESOLVED
Closed: 23 years ago
Resolution: --- → INVALID
verifying
Status: RESOLVED → VERIFIED
You need to log in before you can comment on or make changes to this bug.