如何在关闭子窗口时调用父窗口中的函数



我有一个父窗口和一个子窗口。我想在关闭子窗口时调用一个函数
父窗口的代码如下:

function Button4_onclick(){
newWin=window.open("http://10.10.19.22:8086/childwindow.jsp");
newWin.onunload = updatet();  
}
function updatet()
{
if(newWin.location != "about:blank"){
document.getElementById("SerialNumber24").value='2345';
}

但这段代码不起作用,而是在打开子窗口时调用该函数。那么,我可以使用哪个事件侦听器来关闭呢?Thx。

如果您拥有子窗口,这是可能的。您需要在子级上设置事件onbeforeunload

工作小提琴:https://jsfiddle.net/itgoldman/9mafwu2L/33/

window.foo = function foo() {
alert("hello i am parent")
}
function open_window() {
var win = window.open("", "Title", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=600,height=200,top=" + (screen.height - 400) + ",left=" + (screen.width - 840));
if (!win) {
alert("window open was blocked");
return;
}
win.document.write(str);
win.window.parent = this;
}
var script = `
window.onbeforeunload = function(ev) {
window.parent.foo();
return;
}
`;
var str = '<scri' + 'pt>' + script + '</scr' + 'ipt><h1>close me</h1>'

最新更新