Closed Bug 287894 Opened 19 years ago Closed 19 years ago

window.onload event handler fires at the wrong time

Categories

(Core :: JavaScript Engine, defect)

x86
Windows XP
defect
Not set
major

Tracking

()

RESOLVED INVALID

People

(Reporter: spaligo, Unassigned)

Details

User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6) Gecko/20050317 Firefox/1.0.2
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6) Gecko/20050317 Firefox/1.0.2

If the event handler is assigned in the body tag (<body onload="">) it works
just fine. If you assign the handler in a script in the head tag (in Firefox),
the event fires as it is loaded rather than when the page is done. See the code
below.

Reproducible: Always

Steps to Reproduce:
//event triggers
document.onload = HideJSError();

//functions
function HideJSError()
{
	if (document.getElementById("jserror"))
	{document.getElementById("jserror").style.display = "none";}
}
Actual Results:  
The code in the IF statement doesn't run because the element couldn't be found
(because the body hasn't been loaded yet).
First, this isn't a JavaScript Engine issue but is instead probably DOM 0. The
JavaScript Engine deals solely with issues related to the implementation of the
JavaScript language and is independent of the use of the language in the
browser. Please read up on the products and components in Bugzilla to understand
the proper location for filing bugs.

Second, in document.onload = HideJSError() you are not assigning a reference to
the function you wish to use as the onload handler but are instead calling the
function and assigning its return value to document.onload. 

Third, document.onload doesn't work the way you think. Use window.onload
instead. For example, window.onload = HideJSError;

Invalid.
Status: UNCONFIRMED → RESOLVED
Closed: 19 years ago
Resolution: --- → INVALID
You need to log in before you can comment on or make changes to this bug.