body.onunload event does not fire

S

shripaldalal

Hi,

this works:
<html><body onunload="alert('bye');"></body></html>

THIS DOES NOT WORK:
<html><head><script language=JavaScript>
document.body.onunload = saybye();
function saybye() { alert("bye"); }
</script></head><body></body></html>

document.body.onload = function() works but document.body.onunload =
function() does not work. Can someone throw more light on this ?
Thanks. Bye.

Regards,
Shripal.
 
E

Evertjan.

wrote on 18 jan 2009 in comp.lang.javascript:
Hi,

this works:
<html><body onunload="alert('bye');"></body></html>

THIS DOES NOT WORK:
<html><head>
<script language=JavaScript>

document.body.onunload = saybye();

That will load the result value of the executed saybye() in onunload,
and onunload will not know what to do with that.
Better link the saybye() function pointer to onunload with:

document.body.onunload = saybye;
 
R

RobG

Hi,

this works:
<html><body onunload="alert('bye');"></body></html>

THIS DOES NOT WORK:
<html><head><script language=JavaScript>
document.body.onunload = saybye();
function saybye() { alert("bye"); }
</script></head><body></body></html>

document.body.onload = function() works but document.body.onunload =
function() does not work. Can someone throw more light on this ?

Even if the inappropriate call operator is removed, your code, being
global code in the head element, may well be executed before the body
element exists. Even it isn't in some circumstances, it will be in
others.

Attach listeners to the body element at a point when you are sure that
it will exist. And even then, check that it does exist before
attempting to attach the listener.
 
D

dhtml

Evertjan. said:
wrote on 18 jan 2009 in comp.lang.javascript:
Hi,

this works:
<html><body onunload="alert('bye');"></body></html>

THIS DOES NOT WORK:
<html><head>
<script language=JavaScript>

document.body.onunload = saybye();

That will load the result value of the executed saybye() in onunload,
and onunload will not know what to do with that.
Better link the saybye() function pointer to onunload with:

document.body.onunload = saybye;

Which browser did you test that in?

The problem is that body.onunload is not the same thing as the content
attribute, <body onload="...".

The latter points to window.onload. The former augments the body element
with an |onunload| property.

We've discussed this here before.
 
E

Evertjan.

dhtml wrote on 21 jan 2009 in comp.lang.javascript:
document.body.onunload = saybye;

Which browser did you test that in?

The problem is that body.onunload is not the same thing as the content
attribute, <body onload="...".

The latter points to window.onload. The former augments the body element
with an |onunload| property.

Damn, you're right, used the headbrowser
 
D

dhtml

Evertjan. said:
dhtml wrote on 21 jan 2009 in comp.lang.javascript:
document.body.onunload = saybye;

[..]
Which browser did you test that in?

The problem is that body.onunload is not the same thing as the content
attribute, <body onload="...".

The latter points to window.onload. The former augments the body element
with an |onunload| property.

Damn, you're right, used the headbrowser

Me too :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,114
Latest member
GlucoPremiumReview
Top