Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Window Close Event of Browser

, 16 Feb 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
"Window close event of browser"...

"Window close event of browser"... people are searching for this key sentence for long and have found some solutions too. But sorry to admit that there is no direct support for detecting the window close event of browser or perhaps browser's tab in HTML or JavaScript. All we have are a few nice tricks of HTML DOM (document object model) event which are used to simulate the browsers close event. Not all the codes work fine, or we can say that not all code works in all conditions.

onbeforeunload or onunload

Two of the most popular events of HTML DOM that are used to detect browser's close event are "onunload" and "onbeforeunload". Using onunload is pretty straight; just subscribe the JavaScript event. "onunload" works okay on Internet Explorer, but doesn't work fine in Mozilla Firefox. On the other hand, "onbeforeunload" works okay on both Internet Explorer and Firefox, so using "onbeforeunload" is safe. Let's look at a straight example how this is accomplished.

<script language="JavaScript">
window.onbeforeunload = WindowCloseHanlder;
function WindowCloseHanlder()
{
window.alert('My Window is reloading');
}
</script>

As you can see, this approach is pretty straight and is supposed to work. But it doesn't work always, in fact it doesn't work at all! Well these two events fire each and every time on these operations.

  • post back
  • navigate to another page
  • refresh
  • close the browser
  • close the current tab

There could be more cases that I haven't encountered yet or failed to mention, but these are the most renowned cases. As you can see that the first three cases happens very frequently, so this technique does not work. Yet there are other solutions for overcoming the problem. There is a forum discussion on that here. You can find more discussion if you Google it.

Extending the onbeforeunload Technique

If we handled the case of postback, navigate and refresh, "close event" detect will be fulfilled. So we are going to use some old fashioned technique to handle that. If you want to perform any operation in HTML using mouse, you have to click on some kind of HTML element to do that. And that's why we will trap the mouse down event. We will mark any kind of mouse down on link, button, and any element so that when post back or navigate we can detect that, it's happening because of user interaction on page. Next we will trap any special key event to make the page refresh or reload, for instance pressing Ctrl+F5, F5 and Ctrl+R can make the page refresh so we will also mark such kind of keyboard operation. So we will mark any kind of keyboard event and any kind of mouse event, other than those already mentioned in case we are not marking the Alt+f4 or the browser or tab close. So if our window close event fires, it happens definitely because of closing the browser or closing the tab. Below is a simple code snippet that is given to demonstrate the idea.

<body onbeforeunload="doUnload()" onmousedown="somefunction()">
<form id="form1" runat="server">
</form>
</body>
<script language="javascript" type="text/javascript">
var isClose = false;
//this code will handle the F5 or Ctrl+F5 key
//need to handle more cases like ctrl+R whose codes are not listed here
document.onkeydown = checkKeycode
function checkKeycode(e) {
var keycode;
if (window.event)
keycode = window.event.keyCode;
else if (e)
keycode = e.which;
if(keycode == 116)
{
isClose = true;
}
}
function somefunction()
{
isClose = true;
}
function doUnload()
{
if(!isClose)
{
alert('window is closing');
}
}
</script>

Conclusion

As we have already discussed that there is no 100% fool proof way to detect the browser close event in a lot of cases, the above technique can fail. For example if the user kills the browser's process from task manager, there could be many more cases. Yet in a lot of cases, this technique can be handy. Best of luck and happy programming.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Rahman Masudur
Software Developer Riteq
Australia Australia
About Md. Masudur Rahman
 
Masudur currently works at Riteq as a software developer. Masudur Lives in Sydney, Australia.
 
Awards
 
26 May 2009: Monthly competition: Best ASP.NET article of April 2009
 
24 Mar 2009: Monthly competition: Best ASP.NET article of February 2009
 
Masudur Blog
 
Masudur put down his interesting learning experiences in his blog at http://munnaondotnet.blogspot.com/.

Comments and Discussions

 
GeneralMy vote of 4 PinmemberAmir Mehrabi-Jorshari2-Aug-13 10:25 
QuestionThis does not work for me in the iPhone Safarai and Chrome browser!! Pinmemberamarasat2-Oct-12 11:54 
AnswerRe: This does not work for me in the iPhone Safarai and Chrome browser!! PinmemberRahman Masudur6-Oct-12 20:37 
GeneralRe: This does not work for me in the iPhone Safarai and Chrome browser!! Pinmembersozturk_198112-Mar-13 5:50 
GeneralRe: This does not work for me in the iPhone Safarai and Chrome browser!! Pinmemberaroraamit818-May-13 22:35 
QuestionClose event in chrome PinmemberSweetynewb13-Aug-12 21:46 
GeneralMy vote of 5 Pinmemberkanalbrummer20-May-12 23:05 
QuestionCode doesn't work Pinmemberavay7-Dec-11 22:42 
GeneralMy vote of 5 Pinmembershanawazway14-Nov-11 20:21 
GeneralMy vote of 5 Pinmembermohit_ranjan14329-Jan-11 2:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 16 Feb 2010
Article Copyright 2010 by Rahman Masudur
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid