如何检查使用 window.open() 打开的选项卡是否仍在运行、关闭或刷新?

问题描述 投票:0回答:1
 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);
javascript next.js window.open
1个回答
0
投票

想要将侦听器添加到使用 window.open() 创建的 newWindow 中吗?

© www.soinside.com 2019 - 2024. All rights reserved.