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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:08:49  来源:igfitidea点击:

How can I check If I have access to window.opener?

javascriptwindow.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.documentis 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 getElementByIdreturns 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.openeror window.opener.documentis "falsey" (nullor undefinedor 0or ""or NaNor, of course, false-- and those last four don't apply), resulting in inputbeing undefined. The second assignment will short-circuit if inputis falsey, resulting in valuebeing undefined. undefined!= "1", so...

使用了奇怪的强大&&运算符(近亲的好奇强大的||运营商)。第一次分配会短路,如果window.opener还是window.opener.document为“falsey”(nullundefined0""NaN或,当然,false-那些过去四年不适用),导致inputundefined。如果input为 falsey,则第二个赋值将短路,结果valueundefinedundefined!= "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.getElementByIde.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") {
                }
        }
}