...

View Full Version : Detecting Browser close event in Javascript



rajuds
04-21-2004, 10:27 AM
Hi...
Is there anyway to differentiate between refreshing a window and closing the browser.
It seems both events are handled by onUnload event.

Thanks

jbot
04-21-2004, 10:43 AM
not in IE there isn't, since there's no way to trap how the event bubbles up the event hierarchy.

you might be able to do this in Moz tho.

however, there's no point doing it in one browser and not the other, especially when that browser is the marjority browser. :rolleyes:

rajuds
04-21-2004, 10:51 AM
Hi...
Some websites are using the following script to detect whether window is closed or not.
if(window.screenTop > 10000)
alert("Window is closed");
else
alert("Window stillOpen");

It works fine in IE6.

Can anyone clarify on the Magic number "10000".

glenngv
04-21-2004, 10:57 AM
This works in IE too.


function doUnload()
{
if (window.event.clientX < 0 && window.event.clientY < 0)
{
alert("Window is closing...");
}
}
...
<body onunload="doUnload()">

jbot
04-21-2004, 11:09 AM
neither methods worked for me! :confused:

rajuds
04-21-2004, 11:16 AM
This works in IE too.


function doUnload()
{
if (window.event.clientX < 0 && window.event.clientY < 0)
{
alert("Window is closing...");
}
}
...
<body onunload="doUnload()">


But this code doesn't work if the mouse pointer is placed outside the window and top left corner.!!!!!!!!!!!!!!!!!!!!!!!!

jbot
04-21-2004, 11:34 AM
But this code doesn't work if the mouse pointer is placed outside the window and top left corner.!!!!!!!!!!!!!!!!!!!!!!!!

that's because window.event detects mouse events (*states obvious*) so if the mouse is out of the window's boundaries, then naturally the event won't fire and the function can't be fully executed. :rolleyes:

rajuds
04-21-2004, 11:44 AM
The function is still executing if the mouse is placed in the bottom righthand corner. This is b'coz

The attributes window.event.clientX represents the location of mouse pointer from the starting coordinates of the browser window excluding toobar and menubar. So if the mouse pointer is positioned at top left corner then window.event.clientX is < 0 so the function id excuting.

jbot
04-21-2004, 11:47 AM
if(window.screenTop > 10000)
alert("Window is closed");
else
alert("Window stillOpen");


this would only work with pop-ups tho. you couldn't run it on unload, and expect the "window is closed" alert to execute, because the window would be closed before it could kick in.

jbot
04-21-2004, 11:50 AM
The function is still executing if the mouse is placed in the bottom righthand corner. This is b'coz

The attributes window.event.clientX represents the location of mouse pointer from the starting coordinates of the browser window excluding toobar and menubar. So if the mouse pointer is positioned at top left corner then window.event.clientX is < 0 so the function id excuting.

yes, of the window. not the screen. the user might not be using their mouse, might not even have one. if you presses alt+f4, the script won't execute at all.

glenngv
04-22-2004, 02:32 AM
But this code doesn't work if the mouse pointer is placed outside the window and top left corner.!!!!!!!!!!!!!!!!!!!!!!!!
What do you mean by this?

Try this demo page. Probably step#6 is what you were talking about.
This is tested in IE6 and it works. I don't know why it won't work for jbot.


<html>
<head>
<title>Detecting browser close in IE</title>
<script type="text/javascript">
function doUnload()
{
if (window.event.clientX < 0 && window.event.clientY < 0)
{
alert("Window is closed.");
}
}
</script>
</head>
<body onunload="doUnload()">
<p>
Check which action detects browser window close:
<ol>
<li>Click this <a href="#" onclick="location.reload();return false">Refresh</a> link or the browser's Refresh button</li>
<li>Navigate away from this page through a <a href="http://www.codingforums.com/">link</a></li>
<li>Type another URL in the address bar</li>
<li>Click Back or Forward button</li>
<li>Click the Close (X) button in the top-rightmost corner of the browser</li>
<li>Click the IE icon in the top-leftmost corner and choose Close. Or simply double-click the icon</li>
<li>Press Alt+F4 key</li>
</ol>
</p>
<p>In IE, the last 3 actions are correctly detected by the <a href="#" onclick="alert(doUnload);return false">javascript code</a> inside this page as browser close.</p>
</body>
</html>

jbot
04-22-2004, 09:23 AM
still doesn't work for me. :confused:

i'm using IE 6.0.28 on Win-XPP.

wot about you?

glenngv
04-22-2004, 09:51 AM
IE 6.0.2800.1106 SP1 on NT Server

jbot
04-22-2004, 10:21 AM
interesting.

i have noticed anomalies with IE before. my own company's website, for example, works fine on XPP but not on XPH.

Could this be a security issue? That is, any security fixes for IE or XP might have turned this facility off in my version or on my machine. Would be worthwhile finding out.

Kinda goes to show that you can never be too sure how accurate your code is, without ample development recoverage. :rolleyes:

Willy Duitt
04-22-2004, 04:25 PM
FWIW: It doesn't work for me either. (IE 6.0.2800.1106)

.....Willy

croky
05-11-2004, 08:36 PM
Actually, you can make it work for IE but it's a pain for netscape and mozilla because they don't handle the onunload event when closing windows very well. With NS and Mozilla i allmost did it by coding a function to test for an object on the page, like an image, and then set a flag on the page but the best i did was just controlling the refresh event not the close event.
When closing a window, the trick on IE is to use the onbeforeunload handler event to set a flag and then use onunload to manage the closing procedure.

<body onbeforeunload="ConfirmClose()" onunload="HandleOnClose()">

(javascript)

var myclose=false;
function ConfirmClose() {
if (event.clientY < 0 && event.clientX < 0) {
if (window && !window.closed){
event.returnValue = 'Any message you want';
}
setTimeout('myclose=false',100);
myclose=true;
}
}

function HandleOnClose() {
if (myclose==true) {
alert("Window is closed");
}
}

Try removing the "if" condition handling the modal window (where it says 'Any message you want'). I put it there as an example to control the closing of the window and because i don't know if it works with out it :D
Good luck!

croky

jbot
05-12-2004, 09:35 AM
still doesn't work.

did you even bother to test it?

pressing "alt+f4" or clicking on the "X" button, just closes the window without being prompted or alerted to anything. :rolleyes:

croky
05-12-2004, 10:55 AM
still doesn't work.

did you even bother to test it?

pressing "alt+f4" or clicking on the "X" button, just closes the window without being prompted or alerted to anything. :rolleyes:

Can you post how you build the code ?
I hope you just didn't copy&paste the code from my post, it isn't structured as it should on the page.
Anyway, if you aren't prompted for anything, maybe it's the "if" condition.
Try just " if(!self.closed) " instead of "if (window && !window.closed)"

croky

croky
05-12-2004, 11:08 AM
oh, about myself trying the code, yes and no. I striped the code from a major code block i made and changed it a little, just to show the main idea about this issue. The original function does alot more than this and yes, it works.
Btw, this is working with our cgis for some time.
Alt+f4 and Ctrl+w work.

croky

jbot
05-12-2004, 01:15 PM
Can you post how you build the code ?

ok, here it is:


<html>
<head>
<title>Detecting browser close in IE</title>


<script type="text/javascript">

var myclose = false;

function ConfirmClose()
{
if (event.clientY < 0 && event.clientX < 0)
{
if (!self.closed) event.returnValue = 'Any message you want';

setTimeout('myclose=false',100);
myclose=true;
}
}

function HandleOnClose()
{
if (myclose==true) alert("Window is closed");
}

</script>
</head>


<body onbeforeunload="ConfirmClose()" onunload="HandleOnClose()"

<h4>Close browser!</h4>

</body>
</html>

and still doesn't work.
:confused:

croky
05-12-2004, 03:27 PM
Hell yeah, sorry for my lack of time. After a short debug i have a working script, enjoy ;)


<html>
<head>
<title>Detecting browser close in IE</title>


<script type="text/javascript">

var myclose = false;

function ConfirmClose()
{
if (event.clientY < 0)
{
event.returnValue = 'Any message you want';

setTimeout('myclose=false',100);
myclose=true;
}
}

function HandleOnClose()
{
if (myclose==true) alert("Window is closed");
}

</script>
</head>


<body onbeforeunload="ConfirmClose()" onunload="HandleOnClose()">

<h4>Close browser!</h4>

</body>
</html>

croky

jbot
05-12-2004, 03:56 PM
yep, works now. that would be quite good for determing if the user was trying to leave your secure application and you could then force logout if they did.

we need to work out a Mozilla version now instead. :thumbsup:

brothercake
05-12-2004, 04:13 PM
All of this is unreliable anyway - what if someone has javascript disabled?

There's no way to detect what you want with any degree of certainty, and that being the case, you're still going to have to manage your logins with timed logout or whatever, so you might as well just use that and save yourself the hassle of balancing your security on something unreliable.

jbot
05-12-2004, 05:54 PM
All of this is unreliable anyway - what if someone has javascript disabled?

There's no way to detect what you want with any degree of certainty, and that being the case, you're still going to have to manage your logins with timed logout or whatever, so you might as well just use that and save yourself the hassle of balancing your security on something unreliable.

all too true. but you could use it in addition.

having a forced-logout solution means that even tho the browser which the user was using has closed, the session on the server might still be open, and therefore potentially still accessible using a some sort of cookie style attack. ie getting the session id from the cookie and appeanding it to the page url in a new window.

Willy Duitt
05-12-2004, 09:56 PM
Doesn't work for me if I close the browser with Ctrl & W and although an alert is thrown if I close the browser using the X, it doesn't work if I navigate away from the page and then close the browser.

saishanker
09-16-2005, 06:16 PM
Based on the reply posted by jbot:-

yep, works now. that would be quite good for determing if the user was trying to leave your secure application and you could then force logout if they did.

we need to work out a Mozilla version now instead.

actually I would like to share my ideas:-

for mozilla I am checking in the following manner in the "ConfirmClose() " function called in the "onUnload" attribute of <Body> element.

function ConfirmClose()
{
usrAgnt = navigator.appName;
usrAgnt = userAgent.toLowerCase();

if(usrAgnt =="netscape")
{
//call any function for processing work

Save();

}
}

It works fine for me with the following cases on Windows XP (Professional):-

1. Alt + F4
2. Close ( X ) button on the rightmost corner of the browser.
3. Ctrl + W

Sai. :thumbsup:

humblecoder
01-08-2008, 06:19 AM
You have to check whether the onbeforeunload event is called from a link click or closing the browser..
Check out this link http://mehmoodbluffs.blogspot.com/2007/10/detecting-browser-event-closing-in.html

the_real_bandit
08-27-2008, 07:38 AM
You have to check whether the onbeforeunload event is called from a link click or closing the browser..
Check out this link http://mehmoodbluffs.blogspot.com/2007/10/detecting-browser-event-closing-in.html

How about using cookie,
if created cookie with no expiration date property, the cookie will be deleted automatically when the browser closed. So the browser closed checking could be done on server side.But it also mean we have to check the existances of cookie with specific name on server.

disbis
05-15-2009, 11:58 AM
This is not working fine for me. The function is calling whenever I try to refresh the page. Also getting java script errors in mozilla.

Kor
05-15-2009, 02:34 PM
This is not working fine for me. The function is calling whenever I try to refresh the page.
Javascript is a client side-language. It can not make the distinction between closing the window, changing the page or refreshing the page. onbeforeonload and onunload sense a single action: changing the client-side session.

silentg
05-03-2011, 06:15 PM
Hell yeah, sorry for my lack of time. After a short debug i have a working script, enjoy ;)


<html>
<head>
<title>Detecting browser close in IE</title>


<script type="text/javascript">

var myclose = false;

function ConfirmClose()
{
if (event.clientY < 0)
{
event.returnValue = 'Any message you want';

setTimeout('myclose=false',100);
myclose=true;
}
}

function HandleOnClose()
{
if (myclose==true) alert("Window is closed");
}

</script>
</head>


<body onbeforeunload="ConfirmClose()" onunload="HandleOnClose()">

<h4>Close browser!</h4>

</body>
</html>

croky

croky,

Why did you get rid of the check "event.clientX < 0"?

Kor
05-03-2011, 07:07 PM
Please, don't dig out 6 years old threads. If you have a problem, open your own thread an put it there. And: you have nothing to tell, thus why did you bother to post that?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum