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
ianshaveAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development Software

From novice to tech pro — start learning today.