Andrew Latham
Andrew Latham

Reputation: 6132

Internet Explorer - Check if permission denied

I have the following code in Internet Explorer 8:

if (window.opener != null && window.opener.foo != null)  window.opener.foo = bar;

Sometimes, window.opener is set. But if users open a popup and then navigate away, the attempt to set a property on it should be avoided.

In Firefox and Chrome, this works, because window.opener becomes null once the user exits or refreshes that window. In IE, however, window.opener is not null, and window.opener.foo gives "Permission Denied" instead of null. Because of this, window.opener.foo != null evaluates to true.

How do I get around this problem, what value matches "Permission Denied" in Internet Explorer?

Upvotes: 1

Views: 7534

Answers (2)

Chris Hilditch
Chris Hilditch

Reputation: 161

If you don't have access to the properties of window.opener in IE, then passing it to Object.keys() will return a 0 length string.

Example usage:

if (window.opener && Object.keys(window.opener).length) {
  // do what you will with window.opener
}

Upvotes: 0

jbabey
jbabey

Reputation: 46647

This is the check I use in IE8:

if (window.opener && !window.opener.closed) {
    // do what you will with window.opener
}

Edit: if you want to display a friendly error, you can try something like this:

try {
    if (window.opener && window.opener.foo != null) {
        window.opener.foo = bar;
    }
} catch (e) {
    if (e.description.toLowerCase().indexOf('permission denied') !== -1) {
        // handle it nicely
    } else {
        // some other problem, let it blow up
        throw e;
    }
}

This allows you to specifically handle the "Access Denied" error, while not hiding any other potential errors.

Upvotes: 1

Related Questions