Javascript 如何获取所有已经打开的子窗口的引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6340160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the references of all already opened child windows
提问by Govind Malviya
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.
我想获取所有已经打开的子窗口的引用。有什么办法吗?我不child = window.open(....)
只是使用window.open(....)
和打开多个子窗口。
回答by Hrant Khachatrian
If you don't want to change your current code, you can simply override window.open()
function:
如果您不想更改当前代码,您可以简单地覆盖window.open()
函数:
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
在调用之前运行此代码window.open()
。所有对打开的窗口的引用都将存储在openedWindows
数组中。您可以随时随地访问它们
回答by T.J. Crowder
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.)
我不相信你能,除非你知道窗户的名字,我猜你不知道。(如果你知道他们的名字,你可以使用window.open("", "name")
来获得对他们的引用。)
The better option is, of course, to remember the reference returned from window.open
in the first place — but you know that. :-)
当然,更好的选择是记住从window.open
一开始就返回的引用——但你知道这一点。:-)
回答by Eugenio F. Martinez Pacheco
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)
好的,我在 Oracle CRM onDemand 中使用了这个问题的答案来禁用从父窗口执行脚本的弹出窗口中的选择,并且它起作用了!(我无法控制弹出窗口的生成,它们是由应用程序框架打开的)
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' :)
尝试:我使用浏览器的开发工具 (F12) 找到了图像的 onclick 属性。有一个 openAssocPopup 方法,然后我知道子窗口的名称:'OccamPopup1' :)
Okay! So let's do some magic (executed at the parent window):
好的!所以让我们做一些魔术(在父窗口执行):
window.open("","OccamPopup1").document.getElementById("frmSearch.AQ").setAttribute("disabled", true);
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...
我认为这可能会有所帮助,因为这个问题对我也有帮助。你是对的。现在我试图将孩子的文档对象包装在父母的 jQuery 对象中,这样我就可以访问整个孩子的 DOM ......但这是另一回事......
回答by Derek Wade
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:
我需要检测一个命名窗口(即 CBCheckout)是否已经打开并使用它:
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.
如果您知道窗口的可能名称,您可以循环浏览这些名称,尝试找到它们。