0

I've got a problem with the onunload event testing script it must fire only on the window onunload but it fires with load as well.. Why? There is something wrong, and I don't know what it is.. Please help

Here is the html code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function hello(){alert("welcome to my page");}
</script>
<script type="application/javascript" src="script.js"></script>
</head>
<body onload="hello()" >
</body>
</html>

and here's the script code

window.onunload = goodbye();
function goodbye(){
alert("Thank you for visiting.come back soon!");
}
//window.addEventListener('unload',goodbye(),false);

I've tried the inline calling with body tag, the window.onunload calling and the listener calling. Nothing works correctly, and I get the "Thank you for visiting. come back soon!" message when the page first loads.

Niklas Wulff
  • 3,497
  • 2
  • 22
  • 43
yoo
  • 5
  • 1

1 Answers1

2

The problem is the following line:

window.onunload = goodbye();

This sets the window.unload event equal to the result of the goodbye() function, and calls the goodbye() function immediately. Change this to:

window.onunload = goodbye;

IN MORE DETAIL:

You call a function by naming it, and passing it zero or more parameters in brackets (eg: goodbye()). When you call a function, it is called immediately, and, if it is used as part of a statement (rather than the whole statement), then ther return value of the function is used where the call was made. So, what you're saying with the line:

window.onunload = goodbye();

is: "fetch me the result of calling the goodbye function, and assign the return value to the window.onunload variable". Calling the goodbye function pops up an alert, but returns null, so you are, in effect, saying, "pop up an alert right now, and then set the window.onunload variable to null".

The window.onunload variable (and all other event variables) expects to be a function, so what you actually want to say here is, "set the window.onunload function to be the goodbye function, so when the system calls the onunload event, it will call the goodbye function", and to do that, you assign the function itself to the variable:

window.onunload = goodbye;

HOWEVER

The onunload event is probably not what you're looking for. This only fires after the page has been unloaded, so most of the UI components are not available to your anymore, including alert. Try looking into onbeforeunload instead (JavaScript + onbeforeunload)

Community
  • 1
  • 1
Gareth Cornish
  • 4,357
  • 1
  • 19
  • 22
  • thanks ..... i've tried it but no msg appears at all. do i need to return the alert ? – yoo Jan 28 '13 at 09:26
  • thank you sooooooo ..... the concept is clear now, and the function runs on it's right place and time :) you rocks – yoo Jan 28 '13 at 10:30
  • 1
    now i got another question ... the window.onbeforeunload = goodbye; works only for mozilla firefox .... why the respond of browsers differ to the onunload and onbeforeunlad events ..... what to do when i need the goodbye function to be executed in all browsers ? – yoo Jan 28 '13 at 11:03