By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,257 Members | 925 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,257 IT Pros & Developers. It's quick & easy.

How to detect that user closes the user-agent window ?

P: n/a
I have read all posts about how to detect that url have changed to new
page and trigger the event handler then eg.
function aidLogout(evt) {
if(evt) {
/* maybe via analyse of evt object i can detect the close of the
browser window */
}
if(window.event) {
/* maybe via analyse of window.event object i can detect the close of
the browser window */
}
var i = new Image();
i.src = "aidlogout.asp?uid=1562&SessionID=ABCDEFGHIJKLMNOP "
} /* assuming that aidlogout.asp will return nothing or empty image */

window.unload = aidLogout;
/* or via
if(window.addEventListener) {
window.addEventListener("unload", aidLogout, false);
} else
if(window.attachEvent) {
window.attachEvent("unload", aidLogout);
}

PS i know about super Gecko onclose event which fires when browser
window is closed, but i could not find such method in IE.

Any help would be appreciated :-)

Dec 2 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
I meant that if i could analyse the window.location.url in
window.onunload event eg.
- compare prevous url and next typed url then i will be able to detect
that user closes the window...
- if the next url will be zero lenght or null or undefined then i am
sure that user has closed the window...

Am i right ?

Dec 2 '05 #2

P: n/a
Ok here is a solution (based on hidden popup window):
We want to detect that page.html was unload'ed by exiting browser
window and not by reloading or going to another page.

Solution (popup from this site must not be blocked):

[page.html]

<html>
<head>
<title>W3C DOM Event Propagation</title>
<script type="text/javascript">
function init() {
// using old syntax to assign bubble-type event handlers
// window.onclick = winEvent;
// document.onclick = docEvent;
window.onunload = onCloseWindow;
//document.onunload = onCloseWindow;
//this.onunload = onCloseWindow;

}
function onCloseWindow(evt) {
spyWin = open('page2.html'/*,
'width=100,height=100,left=100,top=0,status=0'*/);
if(spyWin) spyWin.blur();

}

</script>
</head>
<body id="SETIbody" onload="init()">
<h1>unload testing</h1>
</body>
</html>

[page2.html]

<html>
<head>
<title>W3C DOM Event Propagation</title>
<script type="text/javascript">
function test() {
// using old syntax to assign bubble-type event handlers
// window.onclick = winEvent;
// document.onclick = docEvent;
alert(typeof window.opener);
alert(typeof window.opener.document); /* yelds - unknown in IE, in
Gecko there is no yeld */

}

function wait() {
setTimeout("test()", 150);
}

window.onunload = wait;
window.close();
</script>
</head>
<body >
<h1>unload testing 2</h1>
</body>
</html>

Dec 2 '05 #3

P: n/a
Why don't you just use the closed() boolean property of your window?

Dec 2 '05 #4

P: n/a
VK

Luke Matuszewski wrote:
Ok here is a solution (based on hidden popup window):
We want to detect that page.html was unload'ed by exiting browser
window and not by reloading or going to another page.


Well, it is rather old sniffing trick to know where user are going from
your page. It was one (but not the only one) of reasons why popup
blockers killed the window.open() technics this year. Did you try this
way on any modern configuration from the web (not from your local
drive)? Firefox 1.x anywhere, IE 6.0 XP SP2 etc.

As I said several time, session state in browser is very opposite to a
usual desktop application. You don't ask user to save a file, or stay
on the surrent page or lock some action until another action is
finished. You don't do it because you simply cannot do it in the
browser. And if you find a way then it will be some privacy or security
exploit which will be eventually fixed.

The only way you can handle reliably a session based navigation (where
a transaction goes through a set of page / forms) is the *state
snapshot*
You have this mechanics for IE (userData behavior), you have it now in
FF (XPCOM) and W3C are finally started to put it all together. To give
a closest analogy think of a notebook. Usually you can simply close the
cover and it goes off. But the next time you open the cover and in few
seconds you're getting the system in that exact state you had last
time.

State snapshot works the same way: onbeforeunload system makes unstand
snapshot of all registered "state keepers" (form elements, pictures,
text) and user leaves to wherever she wanted without delay and without
any unwanted harassement from your part.
But the next time she comes back (if ever) the page will be in that
exact state as before leaving including every single switch position in
the form.

It is a bit unusual in comparison of desktop applications management
and it is more labor intensive then simply spit out an alert "Stay here
or your data will be lost". So I predict that people will try first all
other tricks before admit that this is the only way :-)

Of course there is always an old good way with server-side session
file. It is very reliable, but I'm talking about client-side solutions.

Dec 2 '05 #5

P: n/a
Yep... that is true, final snippets are:

[page.html]

<html>
<head>
<title></title>
<script type="text/javascript">
function testingPopup(evt) {
spyWin = window.open('page2.html','testing',
'width=100,height=100,left=2000,top=0,status=0');
if(spyWin) spyWin.blur();
}
if(window.attachEvent) {
window.attachEvent("onunload", testingPopup);
} else {
if(window.addEventListener) {
window.addEventListener("unload", testingPopup, false);
} else {
window.onunload = testingPopup;
}
}
</script>
</head>
<body>

</body>
</html>

[page2.html - testingPopup]

<html><head><script type="text/javascript">
function test() {
if(window.opener) {/* Internet Explorer based browsers */
if(window.opener.closed) { /* IE-based browsers */
var img = new Image();
img.src = "url_to_invalide_session";
}
} else { /* Gecko-based-browsers */
var img = new Image();
img.src = "url_to_invalide_session";
}
window.close();
}
setTimeout('test()',150);
</script></head><body></body></html>

Here [page.html] is a real page with content and other stuff - and if
user close that page by closing browser the [page2.html - testingPopup]
will invalide the session...

I can't test directly the window.opener.closed because in Gecko
browsers window.opener is undefined (only window.opener is not null in
IE browsers).

That's all folks...

PS but it only works like an addon, because it assumes that popups for
this site is not blocked !

So USE IT AS AN ADDON FOR MAKEING REQUEST TO SERVER TELLING THAT
BROWSER WINDOW HAS BEEN CLOSED :D

Bye.

Dec 2 '05 #6

P: n/a
Maybe someone will put it in FAQ in jibbering (Jim Ley :) ) as the
answer for the question:

How to detect that a browser window is closed ?

You can't directly.
Solution if to use spying popup (popup blocking must be disabled for
your site).
An example would be like:

[page.html]

<html>
<head>
<title></title>
<script type="text/javascript">
function testingPopup(evt) {
spyWin = window.open('page2.html','testing',
'width=100,height=100,left=2000,top=0,status=0');
if(spyWin) spyWin.blur();
}

if(window.attachEvent) {
window.attachEvent("onunload", testingPopup);
} else {

if(window.addEventListener) {
window.addEventListener("unload", testingPopup, false);
} else {
window.onunload = testingPopup;
}
}

</script>
</head>
<body>

</body>
</html>

[page2.html - testingPopup]

<html><head><script type="text/javascript">
function test() {
if(window.opener) {/* Internet Explorer based browsers */
if(window.opener.closed) { /* IE-based browsers */
var img = new Image();
img.src = "url_to_invalide_session";
}
} else { /* Gecko-based-browsers */
var img = new Image();
img.src = "url_to_invalide_session";
}
window.close();
}

setTimeout('test()',150);
</script></head><body></body></html>

Dec 2 '05 #7

P: n/a
Maybe someone will put it in FAQ in jibbering (Jim Ley :) ) as the
answer for the question:

How to detect that a browser window is closed ?

You can't directly.
Solution if to use spying popup (popup blocking must be disabled for
your site).
An example would be like:

[page.html]

<html>
<head>
<title></title>
<script type="text/javascript">
function testingPopup(evt) {
spyWin = window.open('page2.html','testing',
'width=100,height=100,left=2000,top=0,status=0');
if(spyWin) spyWin.blur();
}

if(window.attachEvent) {
window.attachEvent("onunload", testingPopup);
} else {

if(window.addEventListener) {
window.addEventListener("unload", testingPopup, false);
} else {
window.onunload = testingPopup;
}
}

</script>
</head>
<body>

</body>
</html>

[page2.html - testingPopup]

<html><head><script type="text/javascript">
function test() {
if(window.opener) {/* Internet Explorer based browsers */
if(window.opener.closed) { /* IE-based browsers */
var img = new Image();
img.src = "url_to_invalide_session";
}
} else { /* Gecko-based-browsers */
var img = new Image();
img.src = "url_to_invalide_session";
}
window.close();
}

setTimeout('test()',150);
</script></head><body></body></html>

Dec 2 '05 #8

P: n/a
As i thinked it over a found one weekness:
1) What if user logged into our site was doing some things and then he
pressed Ctrl + N to open second window within the same session to work
in parallel - doing 2 things on 2 browser windows in the same session.
Then he finshed doing first thing and closed the window. As a result
above hidden popup window will log him out !!! so not finished second
work will be lost, becouse session was invalidated !

As a solution we can use confirm to allow the user to choose is he
still working or not.

Modified solution:

[page2.html - testingPopup]

<html><head><script type="text/javascript">
function test() {
if(window.opener) {/* Internet Explorer based browsers */
if(window.opener.closed) { /* IE-based browsers */
if( ! confirm("Are you still working in SETI project ?")) { /*
SETI as an example */
var img = new Image();
img.src = "url_to_invalide_session";
}
}
} else { /* Gecko-based-browsers */
if( ! confirm("Are you still working in SETI project ?")) { /*
SETI as an example */
var img = new Image();
img.src = "url_to_invalide_session";
}
}
window.close();

}

Dec 2 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.