简体   繁体   中英

is it possible to open a popup with javascript and then detect when the user closes it?

The question is pretty much all in the title.

Is it possible (and how?) to open a popup with javascript and then detect when the user closes it?

I am using jquery within the project so a jquery solution would be good. Cheers!

If you have control over the contents of the pop-up, handle the window's unload event there and notify the original window via the opener property, checking first whether the opener has been closed. Note this won't always work in Opera.

window.onunload = function() {
    var win = window.opener;
    if (!win.closed) {
        win.someFunctionToCallWhenPopUpCloses();
    }
};

Since the unload event will fire whenever the user navigates away from the page in the pop-up and not just when the window is closed, you should check that the pop-up has actually closed in someFunctionToCallWhenPopUpCloses :

var popUp = window.open("popup.html", "thePopUp", "");
function someFunctionToCallWhenPopUpCloses() {
    window.setTimeout(function() {
        if (popUp.closed) {
            alert("Pop-up definitely closed");
        }
    }, 1);
}

If you don't have control over the contents of the pop-up, or if one of your target browsers does not support the unload event, you're reduced to some kind of polling solution in the main window. Adjust interval to suit.

var win = window.open("popup.html", "thePopUp", "");
var pollTimer = window.setInterval(function() {
    if (win.closed !== false) { // !== is required for compatibility with Opera
        window.clearInterval(pollTimer);
        someFunctionToCallWhenPopUpCloses();
    }
}, 200);

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! :)

Try looking into the unload and beforeunload window events. Monitoring these should give you an opportunity to call back when the DOM unloads when the window is closed via something like this:

var newWin = window.open('/some/url');
newWin.onunload = function(){
  // DOM unloaded, so the window is likely closed.
}

如果您可以使用jQuery UI Dialog ,它实际上有一个close事件。

To open a new window call:

var wnd = window.open("file.html", "youruniqueid", "width=400, height=300");

If you just want to know when that window is going to close, use onunload .

wnd.onunload = function(){
    // do something
};

If you want a confirmation from the user before the can close it, use onbeforeunload .

wnd.onbeforeunload = function(){
    return "are you sure?";
};

We do this in one of my projects at work.

The trick is to have a JS function in your parent page that you plan to call when the popup is closed, then hook the unload event in the popup.

The window.opener property refers to the page that spawned this popup.

For example, if I wrote a function named callingPageFunction on my original page, I would call it from the popup like this:

$(window).unload(function() {
    window.opener.callingPageFunction()
});

Two notes:

  1. This should be wrapped in a ready function.
  2. I have an anonymous function because you may want other logic in there

I thinks best way is:

const popup = this.window.open(url.toString());
popup.addEventListener('unload', ()=> console.log('closed'))

是的,处理弹出窗口的 onbeforeUnload 事件,然后使用以下方法调用父窗口上的函数:

window.opener.myFunction()

This worked for me. onunload event will be triggered whenever DOM is unloaded for example if url is changed and previous page DOM is unloaded and DOM for new page is loaded.

var newWin = window.open('/some/url',"Example");
    
newWin.onunload = function(){
     if(newWin.closed){
       // DOM unloaded and Window Closed.Do what ever you want to do here
     } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM