How do I close all child windows opened by a parent window?
July 26, 2011 6:13 PM   Subscribe

Javascript Filter: How do I close all child windows opened by a parent window?

How would I go about telling a browser, via Javascript, "find all the open child Windows (that is, all the open browser windows that were opened *via* the current window) and close them?" It seems like I'd be able to just create an array, populate it with all of the active window's child windows, then iterate over the array, closing each window one at a time. However, I can't seem to make this work.

Also, keep in mind, I can't simply keep track of the windows as they are opened (for example, populate an array as they are opened), as the windows will be opened at different times. In other words, the user will click a button which launches some JS that opens a child window. Then they will maybe work in that new window for 10 or so minutes, then return back to the parent and click the button again to open a different child window.

Thanks in advance for any help you can give!
posted by JPowers to Computers & Internet (8 answers total)
 
Anyway to restructure it and simply have an active div that they work in, rather than opening a child window, to keep everything on one screen?
posted by jsturgill at 6:34 PM on July 26, 2011 [1 favorite]


>Also, keep in mind, I can't simply keep track of the windows as they are opened (for example, populate an array as they are opened), as the windows will be opened at different times.

I don't think that makes a difference. Keep a global array of windows, and just push a window into it anytime the user clicks the button that opens the window. Just make sure you check that the window exists before you try to close it to avoid errors.
posted by pyro979 at 6:44 PM on July 26, 2011 [1 favorite]


Someone asked something similar on Stack Overflow. Short answer: you can't do it unless you store the windows' references in the first place. But I'm not clear on why you can't store them; as long as the user isn't reloading the original page in the main browser window, the (global) variables associated with it shouldn't be lost just because he/she switches to a child window. Define a global array and push each new reference returned by window.open() to it; you just need to be careful to check that the windows still exist before you try to close them (in case the user beat you to it).
posted by ManyLeggedCreature at 6:46 PM on July 26, 2011


... snap!
posted by ManyLeggedCreature at 6:46 PM on July 26, 2011


Expanding from the SO code linked above, if you want to cascade the close operation through the cihldren's children (all such children need to include this script):
var openedWindows = [];
window._open = window.open; // saving original function
window._close = window.close;

window.open = function(url,name,params){
  var w = window._open(url,name,params)
  openedWindows.push(w);
  return w;
}

window.close = function() {
  for (w in openedWindows) {
    //cascade to children
    w.close();
  }
  window._close();
}
I haven't actually tested this code, so any bugs are your problem.
posted by axiom at 8:00 PM on July 26, 2011


Oh, and you may want to wrap the for loop in the close function in try / catch.
posted by axiom at 8:09 PM on July 26, 2011


Another approach would be to have the callee inform the caller whenever it was opened (presuming you have control over the callee). This would let you use regular links rather than JS calls through window.open. Define a registration function (say, register) on the parent that adds the passed window to the child list, then have the callee invoke it:

if (window.parent) window.parent.register(window);
posted by axiom at 8:29 PM on July 26, 2011


Note that you have a misconception in your statement.

The parent window's global variables don't get lost they save state as long as the window is open so having a global "openedWindows" variable should save state the whole time.

Small change to axioms code:
window.close = function() {
  while (w = openedWindows.shift()) {// this both will grab a reference and empty the array
    //cascade to children
    w.close(); // Note in axioms code it should be openedwindows[w].close()
  }
  window._close();
}


posted by bitdamaged at 10:02 PM on July 26, 2011


« Older How can I get into Biotech Grad School after...   |   How Safe Is It To Travel To Tokyo? Newer »
This thread is closed to new comments.