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

后端 未结 8 858
没有蜡笔的小新
没有蜡笔的小新 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:45

    There's one tiny catch I have to mention in relation to the previous mentions of onunload based on my previous experience:

    Opera 9.0.x-9.2.x only runs window.onUnload if the user navigates away from a page. If the user instead closes the window, the event will never fire. I suspect this was done to combat the self-reloading popup problem (where a popup could reopen itself on page close).

    This has most likely persisted to Opera 9.5.x. Other browsers may also implement this, but I don't believe IE or Firefox do.

    0 讨论(0)
  • 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
                });
            }
        });
    });
    
    0 讨论(0)
  • 2021-02-13 18:56

    I tried the watcher approach but ran in to the "permission denied" issue while using this in IE6. This happens due to the closed property not being fully accessible around the event of closing the window ... but fortunately with a try { } catch construction it works though :o)

    var w = window.open("http://www.google.com", "_blank", 'top=442,width=480,height=460,resizable=yes', true);
    
    var watchClose = setInterval(function() {
        try {
            if (w.closed) {
                clearTimeout(watchClose);
                //Do something here...
            }
        } catch (e) {}
    }, 200);
    

    Thank you magnus

    0 讨论(0)
  • 2021-02-13 18:58

    From what I checked the jQuery unload is just a wrapper for the native function. I could be wrong as I didn't dug that deep.

    This example worked for me.

    $(document).ready(function(){
        $(window).unload( function (){
            alert('preget');
            $.get(
                '/some.php',
                { request: 'some' }
            );
            alert('postget');
        });
    });
    

    Remember that some browsers block the window.open requests on unload, IE for example.

    0 讨论(0)
  • 2021-02-13 19:01

    I created a watcher that checks if the window has been closed:

    var w = window.open("http://www.google.com", "_blank", 'top=442,width=480,height=460,resizable=yes', true);
    var watchClose = setInterval(function() {
        if (w.closed) {
         clearTimeout(watchClose);
         //Do something here...
        }
     }, 200);
    
    0 讨论(0)
  • 2021-02-13 19:02

    You'd have to have the onBeforeUnload event call a method to notify your handler.

    See this page for a demo.

    http://www.4guysfromrolla.com/demos/OnBeforeUnloadDemo1.htm

    0 讨论(0)
提交回复
热议问题