Solved

Detecting browser closing and opening another page

Posted on 2003-10-22
7
23,123 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Timer 6 99
Looks good in lg screen, md screen but disappears on mobile 4 95
What xml editor to use. 8 101
Summernote required 3 232
Now that Expression Web 4.0 (http://www.microsoft.com/expression/products/Upgrade.aspx) is free if you buy or have the full version of Expression Web 3.0, now is the best time to  migrate from FrontPage to Expression Web (http://www.frontpage-to-exp…
Objective of This Article In 1990’s, when I was a budding software professional, I had a lot of confusion about which stream or technology, I had to choose to build my career. In those days, I had lot of confusion like whether to choose System so…
The purpose of this video is to demonstrate how to create a Printer Friendly PDF on a WordPress Page. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome Screenshot” Google Chrome Extension, and SmallPDF.com Log…
The purpose of this video is to demonstrate how to integrate Mailchimp with Facebook. This will be demonstrated using a Windows 8 PC. Mailchimp and Facebook will be used. Log into your Mailchimp account. : Click on your name. Go to Account Setti…

763 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