How to get the references of all already opened child windows

二次信任 提交于 2019-11-26 10:58:52

问题


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.


回答1:


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




回答2:


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




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/6340160/how-to-get-the-references-of-all-already-opened-child-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!