By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,492 Members | 1,289 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,492 IT Pros & Developers. It's quick & easy.

Determining whether a window is open

P: n/a
I have the following situation: Page A opens a window named 'foo'.
Page A then reloads itself. Is there a way for the reloaded Page A to
determine whether there is an open window named 'foo', *without*
calling window.open?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Christopher Benson-Manica wrote:
I have the following situation: Page A opens a window named 'foo'.
Page A then reloads itself. Is there a way for the reloaded Page A to
determine whether there is an open window named 'foo', *without*
calling window.open?


Put it another way, that's a problem of client-side state keeping; usual
approaches should apply (frames, cookies), with the popup telling it has
successfully been opened or closed, storing the information either in a
frame or in a cookie.
HTH,
Yep.
Jul 23 '05 #2

P: n/a
Yann-Erwan Perio <ye*@invalid.com> wrote:
Put it another way, that's a problem of client-side state keeping; usual
approaches should apply (frames, cookies), with the popup telling it has
successfully been opened or closed, storing the information either in a
frame or in a cookie.


I've been using the frame approach when it's convenient, but there are
some places where it isn't, unfortunately. The cookie idea may or may
not be an option (I'll ask in the morning) but it sounds like it may
be workable. Thanks.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #3

P: n/a
Christopher Benson-Manica wrote:
I have the following situation: Page A opens a window named 'foo'.
Page A then reloads itself. Is there a way for the reloaded Page A to
determine whether there is an open window named 'foo', *without*
calling window.open?


Not with javascript.

Any reference your page has to an opened window is lost as soon as you
navigate away from the page - even to re-load it.

A cookie will not help, all it will tell you is that your script noticed
that a window was opened at some time in the past, it can't tell you if
the window is still open. Nor does the absence of the cookie (or the
presence of a cookie saying the window was closed) mean that the window
isn't open (the opener may have been closed before the child window).

Given the unreliability of any potential solution, the exercise is
futile. Try the following code in various browsers:
<script type="text/javascript">

function openFoo(){
Foo = window.open('', 'Foo', 'width=300,height=150')
Foo.document.write('<h1>Hi, I\'m Foo</h1>');
Foo.document.close();
}

function checkFoo() {
var msg = document.getElementById('msg')
msg.innerHTML = 'Can\'t find \'Foo\'';
if ( 'undefined' != typeof Foo ) {
msg.innerHTML = 'Found ' + typeof Foo + ' Foo';
if ( 'object' == typeof Foo && window.constructor ) {
msg.innerHTML += '<br>And it\'s a ' + Foo.constructor;
}
}
}

function closeFoo() {
if ( window.Foo ) {
window.Foo.close();
}
}

</script>
<div style="padding-top: 100px;">
<input type="button" value="Open Foo" onclick="
openFoo(); setTimeout('checkFoo()', 10);
">
<input type="button" value="Check for Foo" onclick="checkFoo();">
<input type="button" value="Close Foo" onclick="
closeFoo(); setTimeout('checkFoo()', 10);
">
</div>
<p id="msg"></p>


--
Rob
Jul 23 '05 #4

P: n/a
RobG <rg***@iinet.net.auau> wrote:
Not with javascript.
That's what I figured, unfortunately.
A cookie will not help, all it will tell you is that your script noticed
that a window was opened at some time in the past, it can't tell you if
the window is still open. Nor does the absence of the cookie (or the
presence of a cookie saying the window was closed) mean that the window
isn't open (the opener may have been closed before the child window).


It might be good enough for my purposes, however; I only need a
reasonable guess that the window is still open, not an absolute
assurance.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #5

P: n/a
ASM
Christopher Benson-Manica wrote:

It might be good enough for my purposes, however; I only need a
reasonable guess that the window is still open, not an absolute
assurance.


the only way I see
is to have a routine (loop) in popup sending regulary
to it's opener a variable to true (known by the page in main window)

Anyway : what that for ?
after refresh, the opener will not more be abble to acces to the popup
i.e. to give it back focus

or more popup functions waiting opener's variables telling him
do that or this ?

--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #6

P: n/a
RobG wrote:
A cookie will not help, all it will tell you is that your script noticed
that a window was opened at some time in the past, it can't tell you if
the window is still open. Nor does the absence of the cookie (or the
presence of a cookie saying the window was closed) mean that the window
isn't open (the opener may have been closed before the child window).


When I suggested cookies I was thinking that the child window would
control the cookie, not the opener. The quick test case below should
demonstrate the idea, it works on IE6 and Mozilla 1.7 and probably other
browsers (but not Opera, because they've always refused to trigger
onunload events when the page is closing).
--- winopen.html ---
<script type="text/javascript">
function Get_Cookie(name) {
if(typeof document.cookie == "string"){
var start = document.cookie.indexOf(name+"=");
var len = start+name.length+1;
if ((!start)&&
(name != document.cookie.substring(0,name.length))){
return null;
}
if (start == -1) return null;
var end = document.cookie.indexOf(";",len);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(len,end));
}else{
return "";
}
}

function op(a){
window.open(a.href, a.target);
return false;
}
</script>

<a href="wintest.html" target="foo" onclick="return op(this);">
Open Foo
</a>
<a href="#" onclick="alert(Get_Cookie('foo')); return false;">
is Foo open?
</a>
---

--- wintest.html ---
<script type="text/javascript">
function Set_Cookie(name,value,expires,path,domain,secure) {
if(typeof document.cookie == "string"){
document.cookie = name + "=" +escape(value) +
( (expires) ? ";expires=" + expires.toGMTString() : "") +
( (path) ? ";path=" + path : "") +
( (domain) ? ";domain=" + domain : "") +
( (secure) ? ";secure" : "");
}
}

window.onload=function(evt){
Set_Cookie("foo", "Popup is open");
}

window.onunload=function(evt){
Set_Cookie("foo", "Popup is closed");
}
</script>
---

The cookie functions are taken from the FAQ notes. Am I missing something?
Regards,
Yep.
Jul 23 '05 #7

P: n/a
ASM
Yann-Erwan Perio wrote:
The quick test case below should
demonstrate the idea, it works on IE6 and Mozilla 1.7 and probably other
browsers (but not Opera, because they've always refused to trigger
onunload events when the page is closing).
even with a setTimeout ?
--- winopen.html ---
suppose winopen.html is the page in popup ?
<script type="text/javascript">
function Get_Cookie(name) {
if(typeof document.cookie == "string"){
var start = document.cookie.indexOf(name+"=");
var len = start+name.length+1;
if ((!start)&&
(name != document.cookie.substring(0,name.length))){
return null;
}
if (start == -1) return null;
var end = document.cookie.indexOf(";",len);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(len,end));
}else{
return "";
}
}
this below wouldn't be in main page (opener)
function op(a){
window.open(a.href, a.target);
return false;
}
</script>

<a href="wintest.html" target="foo" onclick="return op(this);">
Open Foo
</a>
<a href="#" onclick="alert(Get_Cookie('foo')); return false;">
is Foo open?
</a>
---

--- wintest.html ---
<script type="text/javascript">
function Set_Cookie(name,value,expires,path,domain,secure) {
if(typeof document.cookie == "string"){
document.cookie = name + "=" +escape(value) +
( (expires) ? ";expires=" + expires.toGMTString() : "") +
( (path) ? ";path=" + path : "") +
( (domain) ? ";domain=" + domain : "") +
( (secure) ? ";secure" : "");
}
}

window.onload=function(evt){
Set_Cookie("foo", "Popup is open");
}

window.onunload=function(evt){
Set_Cookie("foo", "Popup is closed");
}
</script>
---


other way could be to work with 3 windows
whom one could be the common memory
http://perso.wanadoo.fr/stephane.mor...popup_oui_non/

bot main page in a frame of main window is the best
(main window get common memory)

--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #8

P: n/a
ASM wrote:

Salut Stéphane,
(but not Opera, because
they've always refused to trigger onunload events when the page is
closing).
even with a setTimeout ?
Even with a setTimeout:-) This is a design decision of Opera, no
onunload event is triggered when the window is closed.

Opera should be praised for the attention they put in trying to protect
the end-user, but I sometimes find they're overly protective,
restricting/copying features where they shouldn't. In the case of the
onunload, IIRC, they argued this was done to prevent "good bye" alert
boxes (while a website being that annoying would certainly not retain
its visitors, and as a result would remove the functionality).
suppose winopen.html is the page in popup ?
Nope, this is the contrary:-)
other way could be to work with 3 windows
whom one could be the common memory


Technically yes, but opening a third window would be a pain to the user
(who'd try to close it at once).
Regards,
Yep.
Jul 23 '05 #9

P: n/a
ASM <st*********************@wanadoo.fr> wrote:
Anyway : what that for ?
after refresh, the opener will not more be abble to acces to the popup
i.e. to give it back focus


My understanding is that calling window.open will return a handle to
the named window if it is already open...

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.