What is a Windows 8 'Child Session?

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

  • 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!

  • Answer:

    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?

JPowers at Ask.Metafilter.Com Visit the source

Was this solution helpful to you?

Other answers

>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.

pyro979

Someone asked something similar on http://stackoverflow.com/questions/6340160/how-to-get-the-references-of-all-already-opened-child-windows. 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).

ManyLeggedCreature

... snap!

ManyLeggedCreature

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 functionwindow._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.

axiom

Oh, and you may want to wrap the for loop in the close function in try / catch.

axiom

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);

axiom

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();}

bitdamaged

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.