javascript 如何检查我是否有权访问 window.opener?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9891461/
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 can I check If I have access to window.opener?
提问by Keith L.
How can I check if I have access to window.opener?
如何检查我是否可以访问 window.opener?
I'm getting an error if I open my page in a new window from a file that is not connected with my page (access denied
).
如果我在新窗口中从未与我的页面 ( access denied
)连接的文件中打开我的页面,则会收到错误消息。
Code:
代码:
if (window.opener) {
if (window.opener.document.getElementById('myHidden') !== "undefined") {
if (window.opener.document.getElementById('myHidden').value == "1" && $("#inputXYZ").val() != "1") {
In line 2the error occurs. But only if I open the page from a random page (that of course does not have an input field called "myHidden"). If I open the page from a "valid" page that has such an element, it is working.
在第2行中发生错误。但只有当我从随机页面打开页面时(当然没有名为“myHidden”的输入字段)。如果我从具有此类元素的“有效”页面打开该页面,则它正在工作。
回答by T.J. Crowder
You're comparing an element instance with the string"undefined"
, and you're not checking whether window.opener.document
is present (I don't know whether you have to or not, but it's easy to add). You probably meant:
您正在将元素实例与string进行比较"undefined"
,并且您没有检查是否window.opener.document
存在(我不知道您是否必须这样做,但很容易添加)。你可能的意思是:
// Note: Still not right, see below
if (typeof window.opener.document.getElementById('myHidden') !== "undefined")
...except that that's still not correct, because getElementById
returns null
(not undefined
) when there's no matching element.
...除了这仍然不正确,因为当没有匹配的元素时getElementById
返回null
(not undefined
)。
Here's how I'd do it:
这是我的做法:
var input = window.opener &&
window.opener.document &&
window.opener.document.getElementById('myHidden');
var value = input && input.value;
if (value != "1") {
// Do something
}
That uses the curiously powerful &&
operator (close cousin to the curiously-powerful ||
operator). The first assignment will short-circuit if window.opener
or window.opener.document
is "falsey" (null
or undefined
or 0
or ""
or NaN
or, of course, false
-- and those last four don't apply), resulting in input
being undefined
. The second assignment will short-circuit if input
is falsey, resulting in value
being undefined
. undefined
!= "1"
, so...
使用了奇怪的强大&&
运算符(近亲的好奇强大的||
运营商)。第一次分配会短路,如果window.opener
还是window.opener.document
为“falsey”(null
或undefined
或0
或""
或NaN
或,当然,false
-那些过去四年不适用),导致input
被undefined
。如果input
为 falsey,则第二个赋值将短路,结果value
为undefined
。undefined
!= "1"
,所以...
回答by Matt Fellows
Check if you have access to window.opener.document:
检查您是否有权访问 window.opener.document:
if (window.opener && window.opener.document) {
if (window.opener.document.getElementById('myHidden') !== "undefined") {
if (window.opener.document.getElementById('myHidden').value == "1" && $("#inputXYZ").val() != "1") {
}
}
}
And if you want to be really sure add in a check for window.opener.document.getElementById
e.g.
如果你想确定添加一个检查window.opener.document.getElementById
例如
if (window.opener && window.opener.document && window.opener.document.getElementById) {
if (window.opener.document.getElementById('myHidden') !== "undefined") {
if (window.opener.document.getElementById('myHidden').value == "1" && $("#inputXYZ").val() != "1") {
}
}
}