• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 23266
  • Last Modified:

Detecting browser closing and opening another page

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
ianshave
Asked:
ianshave
1 Solution
 
pstavrinidesCommented:
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
 
pstavrinidesCommented:
opening a new window:

<body onUnload=window.open('http://www.whatever.com'); self.blur();>
0
 
PE_CF_DEVCommented:
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
ianshaveAuthor Commented:
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
 
PE_CF_DEVCommented:
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
 
jimdeaversCommented:
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
 
vzuserCommented:
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now