最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net - Detecting JavaScript popup window close event in browser - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question edited Aug 18, 2021 at 7:40 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Nov 3, 2012 at 5:57 swapnilswapnil 811 gold badge3 silver badges10 bronze badges 4
  • 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 Commented Nov 3, 2012 at 6: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 Commented Nov 3, 2012 at 6:06
  • stackoverflow./questions/3888902/… – ChaosClown Commented Nov 3, 2012 at 6: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 Commented Nov 3, 2012 at 6:14
Add a ment  | 

3 Answers 3

Reset to default 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.','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! :)

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 :-/

$(window).bind('unload', function(){
    // ...
});
发布评论

评论列表(0)

  1. 暂无评论