Check the parent windows state from child.

M

mannerfanner

In order to reload the opener window from a child window I want to
check the parent windows state. The following code works fine in IE
and Mozilla:

if(top.opener && !top.opener.closed)
{
top.opener.location.reload();
}

BUT ... The state check works because IE recognises top.opener.closed
and Mozilla recognises top.opener. BUT IE does not recognise
top.opener and Mozilla does not recognise top.opener.closed.

I have seen this solution used on several sites. It can't be good
practise to fudge two statements together in one statement to make
them work for two different browsers.

Does anyone know of single method for checking window state which
works in IE and Mozilla?
 
H

Homer J. Simpson

Have u tried to check "parent.document.readyState" ?

This should work both with ie and ns (in fact with all recent browsers)
 
R

RobB

mannerfanner said:
In order to reload the opener window from a child window I want to
check the parent windows state. The following code works fine in IE
and Mozilla:

if(top.opener && !top.opener.closed)
{
top.opener.location.reload();
}

BUT ... The state check works because IE recognises top.opener.closed
and Mozilla recognises top.opener. BUT IE does not recognise
top.opener and Mozilla does not recognise top.opener.closed.

How could IE 'recognize' top.opener.closed, but not top.opener? The
first is merely a property reference of the second's object.

What makes you say 'Mozilla does not recognise top.opener.closed'?
I have seen this solution used on several sites. It can't be good
practise to fudge two statements together in one statement to make
them work for two different browsers.

Does anyone know of single method for checking window state which
works in IE and Mozilla?

No fudge at all. By 'state' you presumably meant closed or not; all
window objects expose a .closed property (Boolean) indicating whether
the window has been closed, necessary as these objects persist even
after the window is off the desktop. Attempting to script a closed
window will throw an exception so, always check. Those aren't two
different approaches to checking 'state', they're a typical
illustration of evaluating object/property references one step at a
time to insure you never get beyond *one* undefined identifier.

if (top.opener ----> does 'opener' reference a window object?
&& !top.opener.closed) ----> OK, is it closed, or can I fiddle with it?

Compound Boolean expressions like this 'short-circuit' if any of the
evaluations, moving from left to right, have a result that makes the
overall outcome a forgone conclusion. For a compound &&,this means if
any operand evaluates to false (no point in continuing), and for || if
any operand is true (the entire expression is). You can use this
behavior to 'step gingerly out on a limb', so to speak, without hurting
yourself.

If I understood you correctly.

http://www.webreference.com/js/column6/object.html
 

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,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top