browser close and refresh event problem..
- i want to open a pop up when browser window is closed. I have written a function as
<body onunload="fun()">
This works. popup gets opened on close of browswer window.
But if user clicks refresh then also pop up is opened.
How can i distinguish these 2 events so that only close event will open a pop up and not the refresh of browser.
I searched on google but did not find answer.
Thanks in advance,
Ravindra.
Answers
- You could test to see if the browser is still up by using (window.event.clientX < 0 && window.event.clientY < 0).
That way clicking refreash will not run your function only when the browser is closed.
Example:
<html>
<script>
function doUnload()
{
if (window.event.clientX < 0 && window.event.clientY < 0)
{
alert("Window is closed!");
}
}
</script>
<body onunload="doUnload()">
</body>
</html>
All Replies
Hi Ravindra,
As far as I know, there is no way to distinguish between Refresh and Close for a browser. I did some research from the website and found the work around way is to open up a frameset in a dialog window without a menu bar and have the frameset capture the onbeforeunload event. This way, the only way to trigger the onbeforeunload event is to hit the "X" button or close the window in some other way.
Hope this helps,
Regards,
- You could test to see if the browser is still up by using (window.event.clientX < 0 && window.event.clientY < 0).
That way clicking refreash will not run your function only when the browser is closed.
Example:
<html>
<script>
function doUnload()
{
if (window.event.clientX < 0 && window.event.clientY < 0)
{
alert("Window is closed!");
}
}
</script>
<body onunload="doUnload()">
</body>
</html> Hi rtdev,
Thanks for sharing your ideas about browser Close and Refresh issue.
I tested your sample codes and found the "alert("Window is closed!");" couldn't be raised when refreshing or closing IE browser. But when I changed your sample codes as this - "if (window.event.clientX > 0 && window.event.clientY < 0)", it seems that the "alert("Window is closed!");" can't be triggered when refreshing the web page if I place my mouse in the client area of the IE browser. If I place my mouse in the title area of the IE browser, the "alert("Window is closed!");" will be triggered when refreshing the web page. When I click "X" button to close the current IE browser, the "alert("Window is closed!");" will happen all the time.
Regards,
- Hi rtdev,
pop up on close of browser solution worked for me. Thanks a lot.
Thanks & Regards,
Ravindra. Thanks, Ji Cheng Wang – MSFT for your comments. I was using IE 6.0. Looks like it changed some what in IE 7.0. Both clientX and clientY do not go to a negative number on unload. Also in IE 6.0 if you move your cursor off the IE window and press F5 you will get the message box as well.
I does not seem like there is a nice clean way to do this that will work for all browsers. The main problem is that the onunload event is called whenever the current document is unloaded. This includes:
- Close the current browser window.
- Navigate to another location by entering a new address or selecting a Favorite.
- Click the Back, Forward, Refresh, or Home button.
- Click on an anchor that refers the browser to another Web page.
- Invoke the anchor.click method.
- Invoke the document.write method.
- Invoke the document.open method.
- Invoke the document.close method.
- Invoke the window.close method.
- Invoke the window.open method, providing the possible value
_selffor the window name. - Invoke the window.navigate or NavigateAndFind method.
- Invoke the location.replace method.
- Invoke the location.reload method.
- Specify a new value for the location.href property.
- Submit a form to the address specified in the ACTION attribute via the INPUT type=submit control, or invoke the form.submit method.
See the full msdn documentation on the onunload javascript event.
- Proposed As Answer byIG_2020 Thursday, September 03, 2009 8:43 AM
- Thanks for your further useful information, rtdev. Yes. I agree with you completely. It seems good that your method can help the OP solve his issue.
- Hi rtdev,so this only works for MS IE i believe. Though not perfect but seems to like only IE. Is there a way to get this working in Firefox and Google Chrome ?thanks for the useful information
- is it not possible to cancel the event - control the event bubble ? In a way disable the functionality of the x button.
Then the user would be forced to use the logout link to close the window. - so this only works for MS IE i believe. Though not perfect but seems to like only IE. Is there a way to get this working in Firefox and Google Chrome ?thanks for the useful information
FLVwin is a Flash Fans!


