faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

120 of 205 people (59%) answered Yes
Recently 7 of 10 people (70%) answered Yes


Why is " if (window.opener && !window.opener.closed)" false? I canīt return value from popUp to wind
of the popup window to the parent window?

Apr 5th, 2008 19:25
ha mo, Jean-Bernard Valentaten, Mark Filipak, Danne L, Colin Fraser, Paul Michael Serrano,

> Danne. Whenever you run into a mystery, try this:
>  javascript:alert(window.opener)
> in the location bar of any javascript enabled browser. What you will 
> get back in this case is 'null'. There is your answer: window.opener 
> returns 'null'. Both 'null' and 'undefined' always convert to 'false' 
> in a logical comparison, and of course, false && ...put anything 
> here... is false.
> - Mark
Of course it returns 'null', since your window does not have any 
parent/opener window!
> Well, I have found "document.open" and "window.open"  but the line : 
>             " if (window.opener && !window.opener.closed)" 
> is maybe a spelling error?
> I have always thought the term "opener" refers to either the parent 
> window or the function you used to open the window, but "opener" was 
> not a method, property or event handler of the window object.
opener is in fact a window object (try this, you'll see: alert
(window.opener); please try it in a window that has a parent!!), so it 
returns an object of the type window. An object of this type has a 
property called 'closed', which will return true, if it has been closed.
So Danne's code is correct, but only works if the window has been 
opened by another window! Otherwise window.opener is undefined and will 
be interpreted as false in a logical operation.
By the way you might want to use self.opener instead of window.opener, 
since it is more accurate in some cases.
>Of course it returns 'null', since your window does not have any 
>parent/opener window!
Well, my window does in fact have a parent/opener window. I still get 
the 'object expected' message when I try to run my script. 
function PassBack() { 
   IF (window.opener && !window.opener.closed) 
     window.opener.document.indicationtext_fld.value = text;
Try 'if' instead of 'IF'. -- Mark
On second thought, perhaps I should not make my answer so terse. The 
'object expected' message you are getting is not a status message. It 
is an error message. It is saying that IE's JavaScript interpreter 
expected an object and got instead: 'IF', which it didn't know what to 
do with. 
Why does IE expect an object as the first token on a line? Who knows? 
Some of the error messages don't make a lot of sense and the line and 
character numbers indicated in error messages are often wrong, but 
nevertheless, an error message is what it is -- Mark
Well the answer to that is very easy my friend.
Since js is case sensitive, it tries to find a function (an object!!) 
called IF.
If we define a function by that name, the object searched for is 
present. Let's just do this (for example):
function IF(someBool)
  return !someBool;
Now the code makes sense to the parser. JS ignores whitespaces, so the 
parameters need not directly be attached to the function-call (or 
object-call *g*), but can be separated by a ws (maybe more, haven't 
tried). Calling an object like this:
someFunction(someParameter, someOtherParameter, etc);
is just a convention that makes the code more readable (just like 
separating the parameters by a space after the comma).
Hope that clears any questions ;)