How to safely set window.opener.location OR window.location?
<p>I'm trying to "redirect" from a popup back to its opener window.
This can be done, obviously, with the following code:</p>

<pre><code>function redirectInOpener(url)
{
    window.opener.location.href = url;
    window.close();
}
</code></pre>

<p>However, this won't work in certain circumstances, for instance if the opener window has been closed in the meantime, or if for some reason we're not currently in a popup.
In those cases, I want to simply load the URL in the current window.</p>

<p>Some of these can be handled like this:</p>

<pre><code>function redirectInOpener(url)
{
    if (window.opener) {
        window.opener.location.href = url;
        window.close();
    }
    else {
        window.location.href = url;
    }
}
</code></pre>

<p>But this still doesn't handle all scenario's, such as, depending on the browser, cross-domain issues.  (The parent window still exists, but now has a different domain.)
In IE9, for instance, the above code succeeds, but the parent window shows a popup warning.  If the user denies permission for this "popup" (which isn't a popup), the original popup is gone, and we don't show the url to the user.</p>

<p>Any way to make this work?  (I'd like to be safe: if there's any chance it doesn't succeed, I'd load the URL in the current window.)
Any other issues I need to be aware of?</p>

<p>I added some exception handling to the above code to be safe, but that still doesn't solve the IE9 cross-domain issue.</p>

<pre><code>function redirectInOpener(url)
{
    if (window.opener) {
        try {
            window.opener.location.href = url;
            window.close();
            return;
        }
        catch(e) {
        }
    }

    // Didn't succeed?  Load in current window
    window.location.href = url;
}
</code></pre>

<p>Thanks,</p>

<p>– Michael</p>
