关闭`window.open()`打开的窗口

index.js

function clickHandler() {
  const newWindow = window.open(
    "./sub.html",
    "_blank",
    "left=100,top=100,height=500,width=800"
  );
  setTimeout(function () {
    if (newWindow) {
      newWindow.close(); // 得判断是否成功打开,然后再在 3s 后关闭子窗口
    }
  }, 3000);
}

注意:该方法只对顶层窗口有效,iframe框架之中的窗口使用该方法无效。

原文地址:https://www.cnblogs.com/aisowe/p/15250040.html