Andrew Latham Andrew Latham - 9 months ago 107
Javascript Question

Internet Explorer - Check if permission denied

I have the following code in Internet Explorer 8:

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

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
becomes null once the user exits or refreshes that window. In IE, however,
is not null, and
gives "Permission Denied" instead of null. Because of this, != null
evaluates to true.

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


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 && != null) { = 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.