gpt4 book ai didi

javascript - 如何将关闭事件绑定(bind)到 window.open()

转载 作者:行者123 更新时间:2023-12-03 04:36:21 25 4
gpt4 key购买 nike

我有以下函数可以在弹出窗口中打开网址:

function windowPopup(url, width, height) {
// center
var left = (screen.width / 2) - (width / 2),
top = (screen.height / 2) - (height / 2);

handle=window.open(
url,
"",
"menubar=no,toolbar=no,resizable=yes,scrollbars=yes,width=" +
width + ",height=" + height + ",top=" + top + ",left=" + left
);
}

我希望能够检测到此窗口何时关闭并执行一些代码。如何使用函数中的句柄来绑定(bind)卸载或关闭?

我想知道有人分享到 Twitter 后窗口是否关闭:

windowPopup("https://twitter.com/intent/tweet/?text=Check%20out%20this%20website!&url=https%3A%2F%2Fgoogle.com%2F", 500, 300);

更新

我更新了拉尔的建议:

function windowPopup(url, width, height) {
// center
var left = (screen.width / 2) - (width / 2),
top = (screen.height / 2) - (height / 2);



handle=window.open(
url,
"",
"menubar=no,toolbar=no,resizable=yes,scrollbars=yes,width=" +
width + ",height=" + height + ",top=" + top + ",left=" + left
);

handle.onbeforeunload = function() {
alert('Bye!');
}

}

虽然这对某些弹出窗口有效,但当我关闭 Twitter 窗口时却不起作用......

最佳答案

您正在寻找 beforeunload 事件:

handle.onbeforeunload = function() {
console.log('Bye!');
}

关于javascript - 如何将关闭事件绑定(bind)到 window.open(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43276757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com