我有以下功能在弹出的窗口中打开一个网址:
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
);
}
我希望能够检测到此窗口何时关闭并执行某些代码。如何使用函数中的句柄绑定卸载或关闭?
我想知道一旦有人分享到Twitter后窗口关闭了:
windowPopup("https://twitter.com/intent/tweet/?text=Check%20out%20this%20website!&url=https%3A%2F%2Fgoogle.com%2F", 500, 300);
我更新了Lar的建议:
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!');
}
}
虽然这适用于一些弹出窗口但是当我关闭推特窗口时它没有用...
答案 0 :(得分:1)
您正在寻找beforeunload
事件:
handle.onbeforeunload = function() {
console.log('Bye!');
}