[英]How can I check If I have access to window.opener?
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 2 the error occurs. 在第2行中,发生错误。 But only if I open the page from a random page (that of course does not have an input field called "myHidden").
但只有当我从一个随机页面打开页面时(当然没有一个名为“myHidden”的输入字段)。 If I open the page from a "valid" page that has such an element, it is working.
如果我从具有这种元素的“有效”页面打开页面,它就可以了。
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). 您正在将元素实例与字符串
"undefined"
,并且您没有检查window.opener.document
是否存在(我不知道您是否必须,但它很容易添加)。 You probably meant: 你可能意味着:
// 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
( 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
. 如果
window.opener
或window.opener.document
为“falsey”( null
或undefined
或0
或""
或NaN
或者当然, false
,那么第一个赋值将短路,而后四个不适用),导致input
undefined
。 The second assignment will short-circuit if input
is falsey, resulting in value
being undefined
. 如果
input
为假,则第二个分配将短路,导致value
undefined
。 undefined
!= "1"
, so... undefined
!= "1"
,所以......
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
eg 如果你想确定加入
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") {
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.