I want to get the references of all already opened child windows. is there any way? I am not using child = window.open(....)
just using window.open(....)
and opening multiple child windows.

- 13,627
- 17
- 68
- 94
-
1If you don't store them when you create them, too late. – RobG Jun 14 '11 at 07:20
4 Answers
If you don't want to change your current code, you can simply override window.open()
function:
var openedWindows = [];
window._open = window.open; // saving original function
window.open = function(url,name,params){
openedWindows.push(window._open(url,name,params));
// you can store names also...
}
Run this code before calling window.open()
. All the references to the opened windows will be stored in openedWindows
array. You can access them anywhere you want

- 3,079
- 24
- 30
-
4@Govind: A caveat for this solution - it is a hack and best avoided to prevent future maintenance issues. See: http://stackoverflow.com/questions/6223449/why-is-it-frowned-upon-to-modify-javascript-objects-prototypes/6223589#6223589 – rahulmohan Jun 14 '11 at 12:46
-
sure! to avoid future issues, it's better to rename the function I wrote to something like `window.open2()` :) And to change all the existing `.open()` calls to `.open2()`. But it seems @Govind doesn't want to change anything in his codes... – Hrant Khachatrian Jun 14 '11 at 14:41
-
1The problem is, when we refresh the main window, we lose all data in openedWindows. Any idea to avoid this? – Rodrigo Mar 15 '16 at 15:25
-
In order to not break any code that relies on the default behaviour, this changed window.open() should also return the reference after storing it in the array. – z-boss Feb 03 '17 at 17:37
-
You should actually do `window._open.apply(window, arguments)` since passing in `undefined` for the arguments and not passing in anything yields different behavior. I believe explicitly passing in `undefined` will disable all window features – pushkin May 01 '19 at 21:28
I don't believe you can, unless you know the windows' names, which I'm guessing you don't. (If you know their names, you can use window.open("", "name")
to get a reference to them.)
The better option is, of course, to remember the reference returned from window.open
in the first place — but you know that. :-)

- 1,031,962
- 187
- 1,923
- 1,875
-
+1 good answer, but I have requirement of this kind of approach. – Govind Malviya Jun 14 '11 at 07:31
-
@Govind: Understood. Unfortunately, barring saving the return value or knowing the names assigned to them so you can retrieve them later with `window.open("", "name")`, I don't think there's a third alternative. – T.J. Crowder Jun 14 '11 at 07:32
Ok, I used the answers to this question in Oracle CRM onDemand to disable a select in a popup window executing the script from the parent window, and it worked! (I have no control over the generation of popup windows, they are opened by the application framework)
Let's see how I did it:
Context: In a detail page the user can add some info by clicking in a magnifying glass icon >>> a new window opens containing a search form, but a select is disturbing the administrator: If the user change its default value he/she will gain access to forbidden records!! Oh my God!
First Approach: Disable that select now!!
Attempt: I found the image's onclick attrib with my browser's dev tools (F12). There was a openAssocPopup method, and then i knew the name of the child window: 'OccamPopup1' :)
Okay! So let's do some magic (executed at the parent window):
window.open("","OccamPopup1").document.getElementById("frmSearch.AQ").setAttribute("disabled", true);
I think this may help, as this question helped to me too. You were right. Now i'm trying to wrap the child's document object within the parent's jQuery object so i can gain access to the entire child's DOM... but this is another story...

- 388
- 1
- 10
-
1This doesn't prevent a user from changing the field value either using Javascript (e.g. from the URL bar) or using dev tools the way you did to find the field. If at all possible getting the 'forbidden records' should be prevented by server-side code. I know I'm responding to an old post, and I imagine you've considered these possibilities and taken the only available solution. However, I think it is important that others coming across this be aware of the risk of client-side-only protections. – SimeonJM Oct 29 '14 at 00:54
-
Yes, you are right... but... 1.- Oracle CRM onDemand is on the cloud, and we change things by means of jQuery as long as we only have a few limited tools to customize functionality. 2.-Users are newbies, or Javascript nerds... almost all of them... I suposse they don't waste time hacking the CRM like us (we are their providers, the dev team)... and i don't mind if they do... :P THANKS! – Eugenio F. Martinez Pacheco Nov 13 '14 at 08:03
You would be best to name the windows using a prefix and a counter.
I needed to detect if a named window (i.e. CBCheckout) was already open and used this:
var signupWindow = window.open('','CBCheckout','toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=1,height=1');
try {
if (signupWindow.document.location.href == "about:blank") {
signupWindow.close();
signupWindow = undefined;
}
} catch (e) { }
This recaptured the reference to the named open window. If it didn't exist, you'd see a small window popup for a second.
If you know the possible names of the windows, you can cycle through the names, attempting to locate them.

- 697
- 8
- 11