473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
8 17349
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
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
Why don't you just use the closed() boolean property of your window?

Dec 2 '05 #4
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steve Cosh | last post by:
Hi, Im writting an asp app that tracks the users scores and info (its a training app) to a access database, ive disabled the ie toolbars and everthing so the user must use my nav buttons so i...
7
by: Chris | last post by:
Can anybody tell me how to detect is the user has closed the browser Thank you
1
by: Bob | last post by:
I am developing a database app using vb.net and SQL Server. I am trying to come up with a way I can detect if the user has made any changes/updates to any data currently displayed on the form so that...
0
by: timbobd | last post by:
Using Visual Basic .NET 2003, I have created a combobox with custom code for "autocomplete" functionality and to trigger a DropDown event when the user starts typing. I want to do a database lookup...
6
by: Aziz | last post by:
Here's the problem. On my OrderForm the user can click a link to open up the AddCustomer form to add a new customer, when AddCustomer form closes, it saves the customer to the DataBase. The...
0
by: MarkusR | last post by:
Good day, I have an untyped table. How do I detect if the user is still editing a record when they try to switch tab pages or close the form? I want to tell them to cancel or save their...
11
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post by:
Hi misters, I have an aplication web asp.net 2.0 and I am Trying to detect the close event in browser, which is the best performance for do this ? What's up with Alt+F4, Refresh, user clicks X,...
5
by: =?Utf-8?B?QW5keQ==?= | last post by:
As per the question really. Not trying to stop them leaving, but just detect WHEN they leave the site
36
by: Don | last post by:
I wrote an app that alerts a user who attempts to open a file that the file is currently in use. It works fine except when the file is opened by Notepad. If a text file is opened, most computers...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.