Reputation: 116343
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
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
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
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
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