How would I raise an event (jQuery or vanilla Javascript) when a popup window is closed?

后端 未结 8 859
没有蜡笔的小新
没有蜡笔的小新 2021-02-13 18:06

I want to raise an event when a popup window is closed, or preferably, just before closing. I\'m storing the popup window object as an object, but I don\'t know of any way to bi

8条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-13 18:47

    I think I figured out what's happening:

    When you use window.open it opens a new window with location "about:blank" and then changes it to the url you provided at the function call.

    So, the unload event is bound before the change from "about:blank" to the right url and is fired when the changing occurs.

    I got it working with JQuery doing this:

    $(function(){
        var win = window.open('http://url-at-same-domain.com','Test', 'width=600,height=500');
        $(win).unload(function(){
            if(this.location == 'about:blank')
            {
                $(this).unload(function(){
                    // do something here
                });
            }
        });
    });
    

提交回复
热议问题