Solved

Detecting browser closing and opening another page

Posted on 2003-10-22
7
23,040 Views
Last Modified: 2007-12-19
Hello

Does anyone know how to detect when a user has clicked on the X in the browser?

I want it so that another page is displayed to the user, which logs them out of the system!

From what i have figured out so far...the 'onbeforeunload' event is used....any ideas?????????

Ian
0
Comment
Question by:ianshave
7 Comments
 
LVL 5

Expert Comment

by:pstavrinides
ID: 9606691
What exactly are you trying to do, JavaScript will allow you to capture the onUnload event when you close the browser:
this is the syntax:

<body onUnload='myfunction()'>

and from there you can define actions to take before the page is closed!
0
 
LVL 5

Expert Comment

by:pstavrinides
ID: 9606718
opening a new window:

<body onUnload=window.open('http://www.whatever.com'); self.blur();>
0
 
LVL 6

Expert Comment

by:PE_CF_DEV
ID: 9608171
An age old question which has no good answer...onUnload will run the function regardless of the page being closed or the user navigating to another page.

function launch_spyWin() {
spyWin = open('url of the spywindow
stuff','spyWin','width=100,height=100,left=2000,top=0,status=0');
spyWin.blur();
}
onunload = launch_spyWin;
The spy window html is here:

<html>
<head>
<title>DO NOT CLOSE</title>
<script type="text/javascript" language="javascript">
function check_opener() {
if (opener && opener.closed){
window.alert('Close Detected!');
}

}

onload = function() {
self.blur();
setTimeout('check_opener()',0);
}
</script>
</head>
<body>
</body>
</html>
The logic here is on unload pop open a new window that new window tests to
see if its parent window still exists. If it exists the page was either
reloaded or went to a new page (the coldfusion code posted can be used to
detirmine that), If the parent no longer exists then the browser window was
closed.

0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:ianshave
ID: 9610021
The onUnload event is useless in this case, as it is called everytime the user just presses a button or selects an item from a drop down!!

In a nutshell the application should only display this new window if the user has clicked on the X and only the X (not when a button on the page is pressed, etc...)

The last comment posted will do the job fine, but it will be very annoying for the user, who keeps seeing a pop up all of the time!!!!

Ian
 
0
 
LVL 6

Accepted Solution

by:
PE_CF_DEV earned 250 total points
ID: 9610752
It won't work fine all the time though ...if the user has  a popup blocker I don't think my above mentioned code will work. Also in my case where I use the code (on an intranet application) If the user has 2 windows open and closes one it will "log them out" of the system. To get around this I captured the ctrl+n and passed a parameted "off" which would then keep the spywindow from being used. Its ugly but the best I came up with in a pinch.

The reason we're doing this is to lock a product while it is being edited,if the browser is closed it clears thier locks. If you plan to use it on a general website visitors may not like this much.

There is no exact code for capturing a user clicking the X button. The best you could do is find the cord of the mouse and detirmine if they are around the X, which in short is a pain in the butt.
0
 

Expert Comment

by:jimdeavers
ID: 9931418
You could use the window.onUnload event to determine if the window was closed or just refreshed (or changed locations)

<script for=window event=onunload>
    var top=self.screenTop;
    if (top>9000) {
        alert('window was closed');
    } else {
        alert('window was Refreshed');
    }
</script>
0
 

Expert Comment

by:vzuser
ID: 15158332
In my case, I want to clean up on the server if the user closes the window.  If you use onunload and top > 9000, then doesn't that mean that the window is already closed?  So you can no longer post?

I am using onbeforeunload, but now have the problem of distinguishing between an actual close and a refresh.

    window.onbeforeunload = unloadAndCleanup;
   
    function unloadAndCleanup(){
        document.myForm.action = "cleanup";
        document.myForm.txn.value = "ABANDON_CHANGES";
        document.myForm.submit();
        alert("All changes abandoned.");
    }

The alert is there to buy me just a second to allow it to get to the server and start processing before the browser closes.

How can I distinguish the browser close condition from the refresh condition, before it actually is closed?




0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In this short web based tutorial, I wanted to show users how they can still use the powers of FrontPage in conjunction with Expression Web 3.  Even though Microsoft eliminated the use of Web components, we can still use them with FrontPage and edit …
Introduction In this tutorial, I'll explain how to create an animated progress meter in a wireframe prototype developed using Axure RP 7.0 - a leading prototyping tool for designing web sites and software. (For more information about Axure and gett…
The purpose of this video is to demonstrate how to connect a WordPress website to Google Analytics. This will be demonstrated using a Windows 8 PC Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php :…
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now