[Javascript] access child windows

Paul Novitski paul at juniperwebcraft.com
Fri Aug 7 14:33:14 CDT 2009


At 8/6/2009 11:42 PM, Sarabjot Singh Nannar wrote:
>I have one parent window and this window is popping up messaging 
>windows based on a timer using window.open().
>Now my problem is to access these windows from parent window for 
>pasting messages in it and closing them. I saw one article at:
>
>http://lists.evolt.org/pipermail/javascript/2005-December/009868.html
>
>
>but this doesn't seem to help as it throws an error when accessing the array.


As to your technical question, the window.open() method returns a 
value that represents the child window that's been opened:

         var oChildWindow = window.open();
         oChildWindow.close();

If you need to create more than one child window, you can keep each 
instance separate, for example in an array:

         var aChildWindows = new Array();
         ...
         // create a new window
         aChildWindows[aChildWindows.length] = window.open();

         // create another separate new window
         aChildWindows[aChildWindows.length] = window.open();

         // close all
         for (var i = 0; i < aChildWindows.length; i++)
         {
                 aChildWindows[i].close();
         }

The expression arrayName[arrayName.length] points to a new, 
as-yet-uncreated item in the array.


However, I'd like to criticize your strategy.

First, Troy is right that many people run with pop-ups blocked, a 
pop-up window being a window created by a script without explicitly 
being asked by the human user. Don't use a technique for 
communicating important messages to the user that some users won't 
see. If the error results from an action that can only be taken when 
JavaScript is running, using JavaScript to display an error message 
is appropriate. However, if the problem situation is one that anyone 
can run into even if they're not running JavaScript, then using 
JavaScript to communicate with them is fundamentally flawed. Consider 
writing your application to take place entirely between a server-side 
script and the user, and then add JavaScript to enhance the 
experience for those with scripting enabled. (Google 'progressive 
enhancement' for more on this topic.)

Second, a web page that creates an unceasing series of pop-up windows 
sounds so irritating that I would personally avoid using the website 
at all. If you must use a pop-up, why not continue to use the same 
one with repeat messages?

Third, creating new windows without warning and without the 
cooperation of the user can really frustrate users of assistive 
technology such as screen readers. When a new window appears, 
suddenly the page changes without their having clicked on a link and 
the Back button is "broken" because it can't take them back where 
they'd been. This is a very poor usability & accessibility scenario. 
Don't do it.

I think your best bet is to a) use something like an alert() dialog 
or as Troy suggests a CSS div, b) re-use the same dialog instead of 
creating new ones, c) make sure that all of your users can read the 
message, and d) make sure that all of your users know how to make the 
message go away to return them to the page at hand.

Regards,

Paul
__________________________

Paul Novitski
Juniper Webcraft Ltd.
http://juniperwebcraft.com 




More information about the Javascript mailing list