4zh4r_s4l4ti
4zh4r_s4l4ti

Reputation: 1574

Using Javascript get all the windows that are opened for a domain

I want to know a way in javascript by which i can get all open window for a domain.

I am aware of the following way -

Suppose i am on wind0 and there i have three links and onclick i have written following code -

var win1=window.open('page1','name1');//link1 onclick
var win2=window.open('page2','name2');//link2 onclick
var win3=window.open('page3','name3');//link3 onclick

Above code will open three different windows. I can use win1, win2, win3 objs and save them in an array and use it latter, but suppose my page on wind0 is refreshed and content of the array is lost.

Now I am unable to find out how many windows are opened for that domain.

How can i get all the windows opened for a domain?

Thanks.

Upvotes: 0

Views: 2093

Answers (2)

Daniel A. White
Daniel A. White

Reputation: 190976

I'd bet you could push them into a value stored in local storage, then pop them off on page unload.

Upvotes: 3

John Dvorak
John Dvorak

Reputation: 27307

You can store the reference to that array from all windows. Even though the array was created in the parent window, it can be referenced in the children from their own scope and will not disappear:

parent:

var windowsOpen = [];
w1 = window.open(...);
windowsOpen.push(w1);
w1.windowsOpen = windowsOpen;
...

children:

$(window).on("unload", function(){
  windowsOpen.filter(function(x){
    return x!=window;
  }
}

As long each window can reference the same array, it will be shared among the windows and it can be used for communication.

Upvotes: 0

Related Questions