Forum Moderators: open
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.
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..
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!
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.
It's just that I need opener's name before closing.
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.
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!
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