I am opening a Javascript popup window using window.open()
function and have close button on it .I want to detect the the close event of that window in IE,Firefox and chrome as I want to clear session variables and redirect to some other page. I tried using window.onbeforeunload
event but it is executing at every postback. Any suggestions will be appreciated.
Asked
Active
Viewed 7,799 times
2

Brian Tompsett - 汤莱恩
- 5,753
- 72
- 57
- 129

swapnil
- 81
- 1
- 3
- 10
-
Where do you need to "catch" the popup being closed - in the parent window or in the popup? Where do you need to run code - in the parent window or in the popup? And what does the code need to do? – Ian Nov 03 '12 at 06:03
-
I need to catch event in same popup window.The code clears any session variables that are used and will redirect to some other page after closing the popup. I also tried widow.opener.location in window.onbeforeunload but it does not get redirected. – swapnil Nov 03 '12 at 06:06
-
http://stackoverflow.com/questions/3888902/javascript-detect-browser-close-tab-close-browser – ChaosClown Nov 03 '12 at 06:07
-
So you mean that when the popup is closed, you need to clear session variables and then redirect what? Redirect the parent page to somewhere else? And how do you "clear any session variables"? Do you make an AJAX request? – Ian Nov 03 '12 at 06:14
3 Answers
2
There is a very simple solution to your problem.
First make a new object which will open up a pop like this :
var winObj = window.open('http://www.google.com','google','width=800,height=600,status=0,toolbar=0');
In order to know when this popup window is closed, you just have to keep checking this with a loop like the following :
var loop = setInterval(function() {
if(winObj.closed) {
clearInterval(loop);
alert('closed');
}
}, 1000);
Now you can replace alert with any javascript code you want.
Have Fun! :)

Kshitij Mittal
- 2,698
- 3
- 25
- 40
-
Upped this as it works in IE and Chrome without having to have specific code to deal with IE – Phil Kermeen Jul 31 '15 at 15:17
1
You need an event handler for that there are just these two one could use. So as you said you will have history back and refresh triggering your event too .
Please view this post
Another solution is that you set popup size to fullscreen and make your own close botton. Im sorry javascript is running out of solutions here :-/

Community
- 1
- 1

ChaosClown
- 367
- 2
- 16
-1
$(window).bind('unload', function(){
// ...
});

vyakhir
- 1,714
- 2
- 17
- 21
-
-
-
"I need to catch event in same popup window" means it shouldn't be caught in the parent window like you are – Ian Nov 03 '12 at 06:17
-
-
Well, that doesn't work either. Since this would fire for every time you leave the page (form submission, redirect, clicking link, etc.). It's not as easy as it seems – Ian Nov 03 '12 at 06:22
-
But that means we still need to catch on parent side. Or I didn't entirely understand what the topic starter wants. – vyakhir Nov 03 '12 at 06:25
-
Well, that's what I would think - you have to catch this kind of thing on the parent side. But for whatever reason, the OP needed to do it in the popup. I mean, maybe it's because the javascript code that needs to run is in the popup's page (which is accessible by the parent) but catching the popup right before being closed is hard. – Ian Nov 03 '12 at 06:32