Is there a way to detect the src the iframe is goi

2020-07-27 06:12发布

I have an iframe which I have no control of its content. Inside the iframe there will be redirection, instead of redirected in the top window (which I expect), the redirection always happen in the iFrame. I tried the solution here: How to get redirect url before onload happens if no control on the redirection? but the outcome is not ideal.

Now inside the iframe page I want to use $(window).unload() and change manually the top.window.location.href to force redirection in top window. So does anyone know how to detect the address to be redirected to after the click and before the onload of the redirected page? Thanks!

PS: The redirected page is of same domain.

1条回答
Fickle 薄情
2楼-- · 2020-07-27 06:55

I hoppe this can help you, it only work in same domain iframe content, the new attribute sandbox will extend the possibilities but for now is only implemented by Chrome.

1)Detect when the IF is loaded

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

2)Get a ref to IF Window and register a listener for the onBeforeUnload event each time the iframe is loaded.

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

3)When you handler(ifOnUnload) will called is to early for get the ifContentWindow.location, then set a delay to read it.

setTimeout(delayedIFUnload, 100);

4)In delayedIFUnload function get the if location and redirect your main page.

window.location = ifContentWindow.location;

I only test the code in my environment(MAC, Chrome) you need do do some work to adjust the code for others browsers. For example with addEventListener or contentWindow.

Here is the link for the working code http://pannonicaquartet.com/test/testif.html, I try to do it in fiddle but Fiddle have many frames and inside it the code don't work properly. I use a span for messages because in this type of operations the alert is blocked, at least in 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>

Happy new year!!!


<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>
查看更多
登录 后发表回答