const newWindow = window.open(
"",
"_blank",
"width=800,height=600,left=2000,top=0"
);
if (newWindow) {
const specificDivContent = document.getElementById(
`slide-${slideIndex}`
).innerHTML;
上面是我用来创建新选项卡并将现有 div 写入该选项卡的代码。我想在刷新或关闭打开的新窗口时调用 api。 尝试使用下面的这些监听器。
window.addEventListener("beforeunload", function(event) {
// Prevent the user from leaving the page.
console.log('tried closing the window')
event.preventDefault();
});
function checkWindowClosed() {
if (window.closed) {
console.log('window closed')
} else console.log('still open')
}
setInterval(checkWindowClosed, 1000);
想要将侦听器添加到使用 window.open() 创建的 newWindow 中吗?