ianshave
asked on
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
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
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=10 0,left=200 0,top=0,st atus=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.
function launch_spyWin() {
spyWin = open('url of the spywindow
stuff','spyWin','width=100
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()
}
</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.
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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>
<script for=window event=onunload>
var top=self.screenTop;
if (top>9000) {
alert('window was closed');
} else {
alert('window was Refreshed');
}
</script>
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?
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?
this is the syntax:
<body onUnload='myfunction()'>
and from there you can define actions to take before the page is closed!