javascript - 有没有办法在加载之前检测 iframe 将重定向到的 src?

标签 javascript dom iframe

我有一个 iframe,我无法控制其内容。在 iframe 内部会有重定向,而不是在顶部窗口中重定向(这是我所期望的),重定向总是发生在 iFrame 中。我在这里尝试了解决方案:How to get redirect url before onload happens if no control on the redirection?但结果并不理想。

现在在 iframe 页面内,我想使用 $(window).unload() 并手动更改 top.window.location.href 以强制在顶部重定向 window 。那么有谁知道如何在点击之后和重定向页面的onload之前检测要重定向到的地址?谢谢!

PS:重定向的页面是同一个域的。

最佳答案

我希望这可以帮助你,它只适用于相同域的 iframe 内容,新的属性沙箱将扩展可能性,但目前仅由 Chrome 实现。

1)检测IF何时加载

myIF.addEventListener("load",ifOnLoad,true);

2)获取对 IF Window 的引用,并在每次加载 iframe 时为 onBeforeUnload 事件注册一个监听器。

ifContentWindow = myIF.contentWindow;
ifContentWindow.addEventListener("beforeunload", ifOnUnload,false);

3)当您调用处理程序(ifOnUnload)时,要尽早获取ifContentWindow.location,然后设置延迟来读取它。

setTimeout(delayedIFUnload, 100);

4)在delayedIFUnload函数中获取if位置并重定向您的主页。

window.location = ifContentWindow.location;

我只在我的环境(MAC、Chrome)中测试代码,您需要做一些工作来调整其他浏览器的代码。例如使用 addEventListener 或 contentWindow。

这是工作代码 http://pannonicaquartet.com/test/testif.html 的链接,我尝试在 fiddle 中执行此操作,但 fiddle 有很多框架,并且其中的代码无法正常工作。我对消息使用跨度,因为在这种类型的操作中,警报会被阻止,至少在 Chrome 中是这样。

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script language="javascript" type="text/javascript">
        var myIF, spNotify, ifContentWindow;
        function init(){
            myIF = document.getElementById("myIF");
            spNotify = document.getElementById("spNotify");
            myIF.src="testIF_1.html";
            myIF.addEventListener("load",ifOnLoad,true);
        }

        function ifOnLoad(){
            try{
                ifContentWindow = myIF.contentWindow;
                ifContentWindow.addEventListener("beforeunload", ifOnUnload,false);
            }catch(er){
                alert(er);
            }
        }

        function ifOnUnload(){
            try{
                notify(ifContentWindow.location);
                setTimeout(delayedIFUnload, 100);
            }catch(er){
                alert(er);
            }
        }

        function delayedIFUnload(){
            try{
                notify(ifContentWindow.location);
            }catch(er){
                alert(er);
            }
        }

        function notify(what){
            spNotify.innerText = what;
        }
    </script>
</head>
<body onload="init();">
    <div id="dvMsg">Target: <span id = "spNotify"></span></div>
    <iframe  id="myIF" src=" " style="width:1100px;height:700px;" />
</body>

新年快乐!!!

<小时/>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script language="javascript" type="text/javascript">
        var myIF, spNotify, ifContentWindow, lastURL, timerRef;
        function init(){
            myIF = document.getElementById("myIF");
            spNotify = document.getElementById("spNotify");
            myIF.src="testIF_1.html";
            myIF.addEventListener("load",ifOnLoad,true);
        }

        function ifOnLoad(){
            try{
                ifContentWindow = myIF.contentWindow;
                ifContentWindow.addEventListener("beforeunload", ifOnUnload,false);
            }catch(er){
                alert(er);
            }
        }

        function ifOnUnload(){
            try{
                notify(ifContentWindow.location);
                lastURL = ifContentWindow.location;
                timerRef = setInterval(delayedIFUnload, 5);
            }catch(er){
                alert(er);
            }
        }

        function delayedIFUnload(){
            try{
                if(lastURL != ifContentWindow.location){
                    notify(ifContentWindow.location);
                    clearInterval(timerRef);
                }
            }catch(er){
                alert(er);
            }
        }

        function notify(what){
            spNotify.innerText = what;
        }
    </script>
</head>

关于javascript - 有没有办法在加载之前检测 iframe 将重定向到的 src?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14103618/

相关文章:

javascript - 使用AJAX加载元素时重新加载JS函数

javascript - 在地址栏中显示 iFrame url

javascript - 如何动态更改 Youtube 播放器 videoID

javascript - 使用 jquery 根据 id 更改 div 顺序

javascript - 在另一个元素加载后隐藏一个元素? Ajax

javascript - javascript 代码中显示的 this.value 未定义消息

javascript - jQuery $.delegate() 单个选择器多个事件还是 $.bind() 更好的解决方案?

CSS如何不使用样式

javascript - 无法从 jQuery 创建的表中删除行

javascript - NodeJS : TypeError: app. 使用的不是函数