Randomblue
Randomblue

Reputation: 116343

List auxiliary windows

With the function window.open() one can spawn auxiliary windows and tabs.

How can I get a list of the spawned auxiliary windows and tabs of the "parent" page in JavaScript?

EDIT: I'm looking for a way to do this without keeping track of the windows I create as they are created.

Upvotes: 4

Views: 86

Answers (4)

Henrik Karlsson
Henrik Karlsson

Reputation: 5723

I don't think it's possible to do that directly, though you could save the windows in an array:

var wins = [];
function openWindow(win){
   newWin = window.open(win);
   wins.push(newWin);
}

Upvotes: 1

jbabey
jbabey

Reputation: 46657

There is no way to do this in javascript. You need to keep track of them yourself:

var windowArray = [];

// whenever you open a window...
var newWindow = window.open(...);
windowArray.push(newWindow);

// whenever you close a window...
if (opener && !opener.closed && opener.windowArray) {
    // search for your window in the array

    var matchingIndex = -1;

    for (var i = 0; i < opener.windowArray.length; i++) {
        if (opener.windowArray[i] === window) {
            matchingIndex = i;
            break;
        }
    }

    // if your window was found, remove it
    if (matchingIndex !== -1) {
       opener.windowArray.splice(matchingIndex, 1);
    }
}

Upvotes: 4

Alex Kalicki
Alex Kalicki

Reputation: 1533

How about this:

var windowArray = [];
windowArray.push(window.open(yourWindow));

windowArray will store references to all the opened windows or tabs.

Upvotes: 0

Jay
Jay

Reputation: 303

I don't know if there is a built-in way to return child windows and tabs in js but you could create an array to keep track of them by creating an entry in the array every time you call window.open()

Upvotes: 1

Related Questions