Skip to content Skip to sidebar Skip to footer

Detect When An Iframe Starts To Load New Url

How can I detect that an iframe on my page starts to load a new page? Our situation is: When iFrame content starts to change we need to display a 'loading' animation and hide the

Solution 1:

I found a better solution if iframe and the container page is same origin, don't have to put extra code into the inner page:

<iframesrc="same-origin.com"onload="content_finished_loading(this)"></iframe><script>var indicator = document.querySelector('.loading-indicator');
    var content_start_loading = function() {
        indicator.style.display = 'block';
    };

    var content_finished_loading = function(iframe) {
        indicator.style.display = 'none';
        // inject the start loading handler when content finished loading
        iframe.contentWindow.onunload = content_start_loading;
    };
</script>

Solution 2:

I came up with following solution - which is only possible because we control the content of the iframe content and the host-window

Inside the iframe we add following script to the page footer (all pages use the same template, so this is a change to a single file)

<script>window.onunload = function() {
    // Notify top window of the unload eventwindow.top.postMessage('iframe_change', '*');
};
</script>

Inside the host-window we add this script to monitor the iframe state

functioninit_content_monitor() {
    var content = jQuery('.iframe');

    // The user did navigate away from the currently displayed iframe page. Show an animationvar content_start_loading = function() {
        alert ('NOW: show the animation');
    }

    // the iframe is done loading a new page. Hide the animation againvar content_finished_loading = function() {
        alert ('DONE: hide the animation');
    }

    // Listen to messages sent from the content iframevar receiveMessage = functionreceiveMessage(e){
        var url = window.location.href,
            url_parts = url.split("/"),
            allowed = url_parts[0] + "//" + url_parts[2];

        // Only react to messages from same domain as current documentif (e.origin !== allowed) return;
        // Handle the messageswitch (e.data) {
            case'iframe_change': content_start_loading(); break;
        }
    };
    window.addEventListener("message", receiveMessage, false);

    // This will be triggered when the iframe is completely loaded
    content.on('load', content_finished_loading);
}

Solution 3:

There is already a possible solution at: iFrame src change event detection?

However, if you want to manipulate the target page, then it is not needed to touch the iframe-target file content. Just add the correct JS code in parent page and it can access SAME-ORIGIN iframe. I used something like this:

<iframeid="xyz"....>
.....
<script>var iframeElement = document.getElementById("xyz");
var iframe_El = iframeElement.contentDocument || iframeElement.contentWindow.document;
// manipulate iframe_El wih "on complete" events and like that.
....
</script>

more at: https://jsfiddle.net/Lnb1stc8/

Solution 4:

When you are creating the iframe dynamically, include the ONLOAD event like so...

F=document.createElement('iframe');
F.onload=function(){
    UpdateDetails(
        this.contentWindow.document.title,
        this.contentWindow.location
    );
};
F.src='some url';
document.getElementById('Some_Container').appendChild(F);

The onload event will now constantly update the global variables below via the function below, as the user surfs the net:

varThe_Latest_Title='';
varThe_Latest_URL='';
functionUpdateDetails(Title,URL){
    The_Latest_Title=Title;
    The_Latest_URL=URL;
}

Post a Comment for "Detect When An Iframe Starts To Load New Url"