Author Topic: Check if a window exists  (Read 803 times)

0 Members and 1 Guest are viewing this topic.

Offline shane18Topic starter

  • Enthusiast
  • Posts: 272
  • Gender: Male
    • View Profile
Check if a window exists
« on: February 10, 2010, 03:02:45 PM »
How do you check to see if a window exists? Like for example if I pop up a window named SHANE... how do I check to see if window SHANE is still open?
-Shane (TAKE LIFE EASY)

Offline Psycho

  • Guru
  • Freak!
  • *
  • Posts: 7,755
    • View Profile
Re: Check if a window exists
« Reply #1 on: February 10, 2010, 03:57:17 PM »
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)

Code: [Select]
<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>
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

Offline shane18Topic starter

  • Enthusiast
  • Posts: 272
  • Gender: Male
    • View Profile
Re: Check if a window exists
« Reply #2 on: February 10, 2010, 04:09:36 PM »
thanks
-Shane (TAKE LIFE EASY)