简体   繁体   中英

Javascript - If pop-up (by name) is open?

How can you check to see if a pop-up is already open strictly by the original pop-up's name, and not URL, etc.

The pop-up is opened via window.open().

Keep the handle to the window:

var popup = window.open( URL, name, features )

So later you can check whether it's closed by using it's "closed" property.

if (popup.closed) {
    // closed
}
else {
    // still open
}

You can see it working here: http://www.javascripter.net/faq/windowclosed.htm

EDIT

You should be able to do just what Cheery said, but if you would like more detail, I tested this, and it works:

<html>
  <head>
    <script type="text/javascript">
      var popup;
      function openPopup() {
        popup = window.open("http://www.stackoverflow.com", "so", "location=1,status=1,scrollbars=1,width=300,height=300");
      }
    </script>
  </head>

  <body>

    <button onclick="openPopup()">open popup</button>
    <button onclick="checkIfPopupIsOpen()">check for popup</button>

    <script type="text/javascript">
      function checkIfPopupIsOpen() {
        if (popup.closed) {
          alert("it's closed");
        }
        else {
          alert("it's still open");
        }
      }
    </script>

  </body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM