onload event for iframe [duplicate]

2

Question

Let's say that on my site I have iframe reserved to load a possible external page, and this iframe is with display:none , as I could create in callback to fire at the end of loading this page, to make a display:block ?

Note

Remembering that I might want full content, which includes loading images, or partial HTML only. Would it be the same event?

    
asked by anonymous 24.01.2017 / 17:30

1 answer

6

You can use the load event that will be triggered when all resources are loaded:

const iframe = document.getElementById('iframe');

window.addEventListener("load", function(event) {
    console.log('Está tudo carregado');
    iframe.style.display = 'block';
});
iframe {
 display: none; 
}
<iframe id="iframe" width="560" height="315" src="https://www.youtube.com/embed/gQghzrNo68s"frameborder="0" allowfullscreen></iframe>

DOCS

To trigger the event when only partial HTML loading occurs, DOCS :

  

The DOMContentLoaded event fires when the initial HTML document has been completely loaded and parsed, without waiting for style sheets, images, and subframes to terminate loading.

You do:

document.addEventListener("DOMContentLoaded", function(event) {
    console.log("DOM completamente carregado e analisado");
    iframe.style.display = 'block';
});
    
24.01.2017 / 17:45