Determining whether a window is open

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

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?
 
Y

Yann-Erwan Perio

Christopher said:
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.
 
C

Christopher Benson-Manica

Yann-Erwan Perio said:
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.
 
R

RobG

Christopher said:
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>
 
C

Christopher Benson-Manica

RobG said:
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.
 
A

ASM

Christopher said:
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 ?
 
Y

Yann-Erwan Perio

RobG said:
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.
 
A

ASM

Yann-Erwan Perio said:
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.moriaux/truc/popup_oui_non/

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

Yann-Erwan Perio

ASM wrote:

Salut Stéphane,
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.
 
C

Christopher Benson-Manica

ASM said:
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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top