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

  • Thread starter Luke Matuszewski
  • Start date
L

Luke Matuszewski

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 :)
 
L

Luke Matuszewski

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 ?
 
L

Luke Matuszewski

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>
 
V

VK

Luke said:
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.
 
L

Luke Matuszewski

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.
 
L

Luke Matuszewski

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>
 
L

Luke Matuszewski

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>
 
L

Luke Matuszewski

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();

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top