javascript - How can I check If I have access to window.opener? - Stack Overflow

How can I check if I have access to window.opener?I'm getting an error if I open my page in a new

How can I check if I have access to 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).

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

How can I check if I have access to 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).

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

Share Improve this question asked Mar 27, 2012 at 14:22 Keith L.Keith L. 2,12412 gold badges41 silver badges64 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You're paring 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:

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

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

Check if you have access to 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.

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") {
                }
        }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742342491a4425907.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信