Entry
how to check if a window.name is open?
Apr 3rd, 2003 05:17
Russ Locke, carsten dfgb,
The basic premise here is to attempt to open a window with the same
name as the window you are testing for.... what you want to do is
attempt to open the window offscreen.. (at the least.. in a position
which would not be achievable dragging a window around). If the window
does not already exist... the window we create will be in our
predifined position and then we test for that position... If the window
does exists.. the existing window will come into focus and not be in
our predefined position.
This example code works only in IE...
For NS to work... instead of screenLeft, use screenX..
however.. it seems that at some point NS has made it so you can't open
windows offscreen..
<html>
<head>
<script language="javascript">
function opentest() {
strFeatures = "width=1,height=1,left=9999,top=9999";
testwindow = window.open("", "TEST", strFeatures);
if (testwindow.top.screenLeft >= 9999) {
testwindow.close();
alert("TEST window not open");
}
else {
testwindow.focus();
alert("TEST window open");
}
}
function createtest() {
strFeatures = "width=100,height=100,left=50,top=50";
window.open("", "TEST", strFeatures);
}
</script>
</head>
<body>
<button onclick="createtest();">CREATE a test window</button>
<p>
<button onclick="opentest();">TEST for window existing</button>
</body>
</html>