Forum Moderators: open

Message Too Old, No Replies

how to close all spawned child windows

         

broniusm

12:54 am on Jan 6, 2004 (gmt 0)

10+ Year Member



In thinking about .parent and .opener, how can I retrieve a collection of all child/spawned windows to loop through and close them?

This need work only in IE 5+, and I tried window.frames to no avail.

Purple Martin

2:36 am on Jan 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When you spawn a child window, you can keep a reference to it with an object variable (called myWindow in this example) like this:

var myWindow = window.open("", "childWindow", "")

You could use an array of object variables for spawning many child windows. Then when you want to close them all, you can loop through the array.

broniusm

3:13 pm on Jan 6, 2004 (gmt 0)

10+ Year Member



Thanks PM. I was afraid of that.. This is a feature to be added to an existing web app that has mutated out of control, and maybe clean-up and a bit of redesign is the only way to go.

For anyone else watching: an expansion would be to populate an array with references to the new window:


// global variable
var _arrWin = new Array();

...

// each time you open a new window
_arrWin[_arrWin.length] = window.open();

...

// closing all windows
var win;
for (var i in _arrWin) {
win = _arrWin[i];
// check if window wasn't already closed
if (win) {
win.close();
}
}

I think something like that should work.. maybe my if(win) part needs to be if(!win.closed) or sim..

BardSong

8:41 pm on Feb 19, 2004 (gmt 0)

10+ Year Member



Does the page doing the opening need to be a frameset? Will navigation to another page lose the variable "myWindow"? PS: I like the loop idea! Have you succeeded at implementation?

broniusm

9:02 pm on Feb 19, 2004 (gmt 0)

10+ Year Member



BardSong: Good question. Yes, all client variable references will be lost. Working within a frameset is a very manageable solution, or you might even consider an approach used by really annoying online marketers where you pop up a window and resize and hide it off the screen so it appears only as a taskbar item. This could then serve as your master. A third solution might be to have a common collection of variables (as a an object class) that can be referenced each time you open a new window. Be sure to set the onBeforeUnload events to properly handle "what ifs," but if you're getting that technical, you're probably trying to exceed what html/javascript was built to do..

I've not done any of the above (nor even my own solution to which you replied, tbh), but it should work.. :D Do keep us posted!

BardSong

5:04 pm on Feb 20, 2004 (gmt 0)

10+ Year Member



broniusm, thanks for the brainstorm. I inherited an existing app that is complex in terms of window-management. That means unfortunately the first two ideas are out. Funny since I mentioned the first ;). The third I honestly don't understand. If after reading the below, you think it's going to work, please explain.

Here's the full problem description:

The page doing the opening can open two different windows. Both children can access a Logout link that ends the session for all open windows and should therefore close them all too. It is possible to go to the home page, open child one, click the home page and open child two. This results in three open windows.

I was able to learn that if I create all the possible windows from any of the three possible Logout clicks, and then close them all, I almost get the desired results. This solution does take a while as new taskbar items appear (top=5000,left=5000) if the windows weren't open. It works very fast if all the windows are already open. The only dangler is if a child window's Logout is clicked, the parent does not get closed.

This is straight-forward with opener.close(), but sometimes the children pages are accessed from an outside page directly. In this case, all of the window creations and closes are unecessary (time is the only penalty (livable)), whereas I wouldn't want to run opener.close().

I'm trying to figure out a way to sometimes close the opener and sometimes not by testing the existance of and then name if of my opener. By asking the spawned windows lonely question, "Who's my daddy?...if I have one!" :)

Hope this all made sense... The ideal solution is to see if any of the windows exist, then close them if so. It's just that I need opener's name before closing. That and since I changed pages after opening, I lost the reference to the children. To get around that I was opening new windows with the same name and then closing them.

broniusm

4:01 pm on Feb 21, 2004 (gmt 0)

10+ Year Member



It's just that I need opener's name before closing.

Do you really need the name or just a reference to the window?
var mywin = window.open('url', 'winname');
mywin <== the reference
winname <== the name

To check for existence of a parent window (or any declared/defined objects in javascript including functions), you can just check like this:

if (window.varname) {
// it exists
}

I imagine this ought to work for opener as well:
if (window.opener)

That and since I changed pages after opening, I lost the reference to the children.

I'm curious what constitutes "change" in your sentence above.

BardSong

2:01 pm on Feb 23, 2004 (gmt 0)

10+ Year Member



broniusm,

Some great questions and responses. You are absolutely correct in the rewording of "name" to "reference". I loop through all of the references opening and closing them (and make sure I document the names I use throughout the site!).

What I mean by the pages changing is simply navigating to another page. When I open the new window, I can refer to it by reference for the rest of the page; when I move to another page, that reference is lost. The slickest way I found so far is to recreate the reference using another window.open statement assigning the new window to a reference of the same name (mywin). What I'd like to know is, how can I pass in the name to use? Example:

function openWin(winName, winURL, winTitle, winProperties) {
winName = window.open(winURL, winTitle, winProperties);
}

Unfortunately this creates a reference to the literal "winName". Any suggestions?

And yes, I could check for the existance before opening and closing. Your syntax will work fine, and another syntax is: if("newWin" in window).

Again thanks!

broniusm

6:45 pm on Feb 23, 2004 (gmt 0)

10+ Year Member



BardSong-

When I open the new window, I can refer to it by reference for the rest of the page; when I move to another page, that reference is lost.

Ah, if the opener navigates somewhere else, surely the reference will be lost. That's why we started this thread talking about having a control win (ex: called "winmaster") somewhere. I *think* you can refer to this control window from any window globally by name. Check the window.frames [msdn.microsoft.com] documentation, because I think that or window.frames.all is the way to get to windows by names..

If this is the case, you could say something like:

var winmaster = window.frames["winmaster"];
winmaster.arrWin[arrWin.length] = window.open(winurl, etc);

and the document in winmaster would have an array of windows called arrWin through which you could loop to close windows.

You can always talk across documents by referencing the container window by name or reference. I believe names are cross-window-global, and references would have to be declared at least upon init of the window:

"winmaster" is the name of a window whenever you open a window with a target="winmaster" or, I think, also when you say window.open(winUrl, "winmaster").

"winmaster" is a reference when you say var winmaster = window.open(winUrl, winName) or when it is otherwise assigned.

Check the window open method properties [msdn.microsoft.com] under sName for how name works.

still talking theory, hoping I'm not far off base,
bronius