index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
  </head>
  <body>
    <button >打开窗口</button>
    <button >检测</button>
    <script src="./index.js"></script>
  </body>
</html>

index.js

const btn1 = document.getElementById("btn1");
const btn2 = document.getElementById("btn2");
let targetWindow = null;

btn1.addEventListener("click", function () {
  targetWindow = window.open("http://www.baidu.com");
});

btn2.addEventListener("click", function () {
  if (targetWindow) {
    if (targetWindow.closed) {
      alert("窗口已经关闭");
    } else {
      alert("窗口还未关闭");
    }
  } else {
    alert("窗口还未打开过");
  }
});

相关文章:

  • 2022-12-23
  • 2021-12-22
  • 2022-12-23
  • 2021-11-16
  • 2021-12-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-20
  • 2021-07-25
  • 2021-11-20
  • 2021-07-29
  • 2022-12-23
  • 2021-09-20
相关资源
相似解决方案