Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
13
rated 0 times [  18] [ 5]  / answers: 1 / hits: 58617  / 12 Years ago, wed, march 28, 2012, 12:00:00

Consider the following HTML snippet containing some javascript utilizing prompt and unload. The prompt() method works fine but I want alerting something like Goodbye, user when reloading or leaving the page. Any help is greatly appreciated.



<body onload=promptName() >


<script type=text/javascript>
function promptName()
{
var userName = prompt(What's your name ?, )
return userName;
}

function goodBye()
{
alert(Goodbye, + promptName() + !);
}

window.onunload = goodBye();

</script>

</body>

More From » html

 Answers
5

You should write it like this:



window.onunload = goodBye;


Also, you might consider using the onbeforeunload event in some browsers:



window.onbeforeunload = goodBye;


When you write window.onunload = goodBye(); you assign whatever handler that is returned from goodBye to the unload event. Since nothing is returned, there will be no event handler. You need to reference the function instead: window.onunload = goodBye;


[#86562] Tuesday, March 27, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaden

Total Points: 709
Total Questions: 91
Total Answers: 91

Location: Maldives
Member since Sat, Jan 29, 2022
2 Years ago
;