CancelImage Upload

How to find out if a popup is still open or has been closed with JavaScript

If you have a page that calls for functionalities within a pop-up you may need to check to make sure that it is indeed still open, and that the user has not closed it. Here is how the pop-up's status can be checked via JavaScript:

newPopUp = window.open();

if(newPopUp.closed)
{

      //Code for if Pop-Up has been closed.

}
else
{

      //Code for if pop-up is still open.

}

The key here then is that when the pop-up is initialised, it needs to be given a name by which its status can be checked. In this case the name is newPopUp. If it has indeed be closed newPopUp.closed will return true, and if not it will return false.

Here is a more extensive code example, with the current status of the pop-up being written into a span via getElementById():

<html>
<head>
<title>Check Pop-Up Status</title>
<script type="text/javascript">

function popup()
{

      newPopUp = window.open("", "", "width=300, height=150");

      newPopUp.document.write("Your text here");

}


function popUpStatus()
{

      var status = document.getElementById("status");

      if(newPopUp.closed)
      {

            status.innerHTML = "PopUp has been closed.";

      }
      else
      {

            status.innerHTML = "PopUp is still open.";

      }

}

</script>
</head>
<body>

<input type="button" value="Open Pop-Up" onclick="javascritp:popup()" />
<input type="button" value="Check Status" onclick="javascritp:popUpStatus()"><br>
<span id="status"></span>

</body>
</html>


Login
Want to leave a comment?

No problem. Just enter your email and password below.


register | home | reminder

myDesignTool Networking • www.mydesigntool.cominfo@mydesigntool.com