Onload event for iframe

Asked

Viewed 1,567 times

2

Question

Let’s say on my website I have a iframe reserved to upload a possible external page, and this iframe be with display:none, how I could create in callback to fire at the end of loading this page, to perform a display:block?

Note

Remembering that I might want full content, which includes uploading the images, or only partial HTML. It would be the same event?

1 answer

6


You can use the event load, that will be fired 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 happens, DOCS:

The Domcontentloaded event is triggered when the initial HTML document has been fully loaded and parsed, without waiting for style sheets, images, and subframes to finish loading.

Fez:

document.addEventListener("DOMContentLoaded", function(event) {
    console.log("DOM completamente carregado e analisado");
    iframe.style.display = 'block';
});
  • There is some difference when loading images or only html?

  • I put, this addition @Guilhermelautert

  • Perfect, complete :D

  • You’re welcome @Guilhermelautert.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.