How to detect if iframe's source page is fully loaded into DOM

I want an event to fire after iframe's source page is loaded into DOM to do some customization client side. Please help me.

2 answers

  • answered 2019-02-10 12:34 FZs

    Use onload event:

    document.getElementById("iframe-id").addEventListener("load",event=>{
        //Do some stuff when iframe loaded
    })
    

  • answered 2019-02-10 12:34 T.J. Crowder

    If the iframe's content is from the same origin as the containing page, you can listen for the DOMContentLoaded event on it:

    theFrame.documentWindow.addEventListener("DOMContentLoaded", function() {
        // ...
    });
    

    (If the content isn't from the same origin, I don't think you can hook that event — but that doesn't matter, because you wouldn't be able to do the customization you've mentioned, either. :-) )