在 javascript 中打开和关闭弹出窗口

问题描述 投票:0回答:1

我尝试使用 javascript 在简单的 HTML 页面中打开和关闭弹出窗口,但也许我缺少 JS 的基本知识。首先,我尝试声明全局变量 myPopupHandle。 但 myOpenWnd 中未填充帽子。这是代码也许有人可以帮忙。

<head>
  <meta charset="utf-8">
  <title>WndCloseTest</title>
</head>

<body>
  <button type="button" onclick="myOpenWnd()">Open Popup Window</button>
  <button type="button" onclick="myCloseWnd()">Close Popup Window</button>
  <script type='text/javascript'>
    let myPopUpHandle = null; // Always null
    window.addEventListener("onunload", function(e) {
      myCloseWnd();
    });

    function myCloseWnd() {
      if (windowObjectReference != null)
        windowObjectReference.close();
    }

    function myOpenWnd() {
      const windowFeatures = "left=100,top=100,width=320,height=320";
      /*
      myPopUpHandle = window.open(
          "https://www.mozilla.org/",
          "mozillaWindow",
          windowFeatures,
      );
      Don't work popUpHandle is null after this
       */


      const handle = window.open(
        "https://www.mozilla.org/",
        "mozillaWindow",
        windowFeatures,
      );
      if (!handle) {
        console.log("his is likely caused by built-in popup blockers"); // logs the className of my_element
        // The window wasn't allowed to open
        // This is likely caused by built-in popup blockers.
        // …
      } else
        window['windowObjectReference'] = handle;
    }
  </script>
</body>

javascript html popup
1个回答
0
投票

检查您的浏览器是否像您的评论所述阻止弹出窗口。

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