How to get redirect url before onload happens if n

2019-08-08 21:25发布

问题:

I've got an iframe which contains a web part I got no control. The web part contains some redirection functionality.

The problem is, the redirect page loads in the iframe instead of the top window. I tried forcing the redirection to the top window by adding in the head:

<base target="_search" />
<base target="_top" />
<base target="_parent" />

But without any success (I guess it's because there's no any <a></a> inside the iframe?).

So I tried another solution, like in here: iFrame src change event detection? and this is my code:

if (ifrm.attachEvent)
    ifrm.attachEvent('onload', OnIframeRedirect);
else
    ifrm.addEventListener('load', OnIframeRedirect, false);

function OnIframeRedirect() {
    var redirectUrl = this.contentWindow.location;
    if (redirectUrl.pathname.indexOf('PartOfTheIframeSrc') == -1)
    // When the iframe is not loading it's original src
    top.window.location.href = redirectUrl;
}

This worked. But because OnIframeRedirect is happened after onload, users can still see the redirected page before top.window.location changes.

So I would like to know, if anyone has an idea how to get the redirect url before the onload happens?