Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling function with Onunload

Status
Not open for further replies.

Stewart531

Programmer
Feb 18, 2003
36
US
I'm having trouble calling a user defined function when using onUnload, but I have no problem calling a javascript function using onUnload.

<BODY onUnload= alert('Testing')>
This works

<BODY onUnload=&quot;Test()&quot;>
Doesn't Work

function Test()
{
//code
}

Is there something wrong with the syntax?

Thanks,
Dave
 
This works:

<html>
<body onunload=&quot;Test();&quot;>
<script type=&quot;text/JavaScript&quot;>
<!--
function Test(){
alert(&quot;Testing!&quot;);
}
//-->
</script>
</body>
</html>

Rick -----------------------------------------------------------
RISTMO Designs
Arab Church
 
I tried adding the semicolon, but it still won't work
 
Just a question...Stewart, did you actually try putting the alert inside your function to make sure? Rick's script works fine for me. Maybe the problem is the function itself??
 
Stewart,

Under what browser are you running this? IE allows for all kinds of junk to run on an onUnload event, but Netscape and Mozilla are more strict and when you close one of those browsers, the child procs are usually killed as well. So, if you're trying to, say, pop up an alert window, it pretty much ain't gonna happen.

I might be fulla dookie, but this seems to be the case all over.

Cheers,

Edward &quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
It works after all, my fault. I didn't realize that the function declaration was supposed to be included in the html, as I previously had the declaration later in the javascript code. Now I'm having a new problem. It seems like I can't call a setTimeout function inside the function called by onUnload. Any ideas why this is happening?

Thanks,
Dave
 
Sure -- garbage collection. See my previous note. onUnload events have to be quick things because a well-behaved browser knows that a User closing the browser Wants It Gone. If you set up a timeout event, I'd actually be surprised such a thing would ever happen, as it pretty much goes against the whole principle of Killing the Browser.

Cheers,
[monkey] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top