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

php - Call some JavaScript when the user closes a (popup) window - Stack Overflow

programmeradmin4浏览0评论

Is it possible to call some JS when the user closes a window?

I searched for JS event handlers and found only onunload, which calls the script whenever the user leaves a page, not necessarily closing the window.

The intended use is to call an AJAX script that unsets a few sessions pertinent to the popup window. There may or may not be a better way of achieving this aim?

I found this DevShed thread in which it is stated that this is possible through JS, but it does not explain exactly how to call the script.

Unfortunately most Google searches for a solution to this bring up window.close, which is effectively the opposite to what I need!

Is it possible to call some JS when the user closes a window?

I searched for JS event handlers and found only onunload, which calls the script whenever the user leaves a page, not necessarily closing the window.

The intended use is to call an AJAX script that unsets a few sessions pertinent to the popup window. There may or may not be a better way of achieving this aim?

I found this DevShed thread in which it is stated that this is possible through JS, but it does not explain exactly how to call the script.

Unfortunately most Google searches for a solution to this bring up window.close, which is effectively the opposite to what I need!

Share Improve this question asked Aug 26, 2009 at 10:48 bcmcfcbcmcfc 26.8k30 gold badges114 silver badges183 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 5

The following code works in Firefox, IE 8, and "Google Chrome".

In the the opening window

<script src="http://ajax.googleapis./ajax/libs/mootools/1.2.3/mootools-yui-pressed.js"></script>
<script>
function on_popup_close(){
  //put your code here
  alert('it closed');
}
</script>

In the popup

<script>
function inform_parent(){
  opener.on_popup_close();
} 

window.onbeforeunload = inform_parent;
</script>

The first line in the code for the opening window can be your favorite framework that implements the dollar sign operator.

See my demo here

you can check from the parent window if the child window still exists (e.g. check every 100 ms or so) and launch script if not...

Have a look at the window.opener property. Except for IE it has pretty good support. You could use it to call some javascript functions in your parent window before window.close()ing your popup.

No idea whether it works in practice, but if the issue is navigating to internal pages, why not set a global variable of some type whenever a link is clicked in page, and check for that onunload?

Something like (excuse the jQuery, I'm extremely lazy):

INTERNAL_CLICKED = false;

$("a").click(function() {
    INTERNAL_CLICKED = true;
});

window.onunload = function() {
    if(!INTERNAL_CLICKED) {
        // perform window unload code
    }
}

To reiterate, pletely untested, but who knows - it may well just work for you. Although, I would beware, that's probably going to fire on refresh as well.

Hi You can use following function on onbeforeunload event fired by window just before the window.close

window.onbeforeunload = function () {
   // stuff do do before the window is unloaded here.
}

let me know if it not solve ur probelm

发布评论

评论列表(0)

  1. 暂无评论