获取另一个(以编程方式打开的)浏览器的关闭事件 window

Getting Close event of another (programmatically opened) browser window

我正在打开一个新的浏览器 window 点击事件。有没有办法从 parent

绑定 child window 的关闭事件

样本html:

<button id="btn">Click</button>
<label id="lbl"></label>

脚本

$(function () {
    $("#btn").on('click', function () {
        window.open("about:blank", "test", "height=200px,width=200px");
        $("#lbl").html("Opend");
    });
});

举例来说,我想在 child 关闭时将标签文本更改为 closed

Js Fiddle Link

使用onbeforeunload事件:

var new_window = window.open("about:blank", "test", "height=200px,width=200px");
new_window.onbeforeunload = function () {
    alert('closed');
}

演示:https://jsfiddle.net/p48eu9ng/1/

试试这个

$(function(){

    $("#btn").on('click',function(){

       var new_window = window.open("about:blank","test","height=200px,width=200px");
        new_window.onbeforeunload = function(){ $("#lbl").html("Closed");}
        $("#lbl").html("Opend");
    });

});

试试这个 fiddle,将标签更改为在关闭新标签后关闭 window。

$(function(){

    $("#btn").on('click',function(){        
    var newWindow = window.open("about:blank","test","height=200px,width=200px");
        $("#lbl").html("Opend");        
    $(newWindow).bind("beforeunload", function() { 
        $("#lbl").html("Closed");
    })    
    });