Jump to content

Check if a window exists


shane18

Recommended Posts

You need to create a reference to the window when opening it. You can then use that reference to see if the window was ever opened, is currently open, or has been closed. Here is a quick script that can work with multiple windows (tested in IE and FF)

 

<html>
<head>
<script type="text/javascript">

var windowObjs = new Array();

function openWindow(windowName)
{
   windowObjs[windowName] = window.open('somepage.htm', windowName);
}

function windowExist(windowName)
{
    if (!windowObjs[windowName])
    {
        alert('The window "' + windowName + '" has not been opened yet');
    }
    else if(!windowObjs[windowName].closed)
    {
        alert('The window "' + windowName + '" is open');
    }
    else
    {
        alert('The window "' + windowName + '" has been closed');
    }

    return;
}

</script>
</head>

<body>
<button onclick="openWindow('test1');">Open Window Test1</button>
<button onclick="openWindow('test2');">Open Window Test2</button>
<br /><br /><br />
<button onclick="windowExist('test1');">Status of window Test1</button>
<button onclick="windowExist('test2');">Status of window Test2</button>

</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.