Closed Bug 301401 Opened 19 years ago Closed 19 years ago

Assigning a function to an event handler (such as onload) dynamically does not work

Categories

(Firefox :: General, defect)

x86
Windows XP
defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 52440

People

(Reporter: dkbat6, Unassigned)

Details

Attachments

(1 file)

User-Agent:       Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50215)
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8b4) Gecko/20050719 Firefox/1.0+

When I try to set an event handler for, say, the BODY element of an HTML page 
by a direct assignment in Javascript, the function that I set to be used for 
that event handler never gets called.  For instance, if I use the following 
HTML page

<HTML>
<BODY>
<SCRIPT>
document.getElementsByTagName("BODY")[0].onunload=unloadBody
function unloadBody()
{
    alert("here")
}
</SCRIPT>
</BODY>
</HTML>

When I navigate to another page or hit the refresh button, I SHOULD see an 
alert come up that says "hello".  However, this never occurs.  In addition, if 
I use the line

document.getElementsByTagName("BODY")[0].onunload=unloadBody()

then things all work as they SHOULD work when using the other statement (the 
different being that this one has parentheses following the function name).  
This syntax should result in something totally different, that is, the 
onunload event handler should be given the value RETURNED from the function 
unloadBody.

In addition to this, the following syntax should work (but doesn't):

document.getElementsByTagName("BODY")[0].onunload=function () {alert("here");}

Once again, this should show an alert upon navigating to another page or 
hitting the refresh button.

Reproducible: Always

Steps to Reproduce:
1. Open the HTML page that is attached to this bug report (or just paste the 
HTML code from above into a file and open that file)
2. Press the refresh button, navigate to a different page, or close the 
browser window/tab. (all of which in turn cause the BODY onunload event to be 
fired)
Actual Results:  
Nothing happens. No alert, no error, no nothing.

Expected Results:  
An alert should have popped up displaying "hello".

I have been told that this bug does not occur in Firefox 1.0.4

*** This bug has been marked as a duplicate of 52440 ***
Status: UNCONFIRMED → RESOLVED
Closed: 19 years ago
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: