Javascript – how can i create an iframe and destroy the element only after the iframe has loaded


i am able to create an iframe using appendChild, without jquery, is it possible for me to know when the iframe has loaded and then be able to destroy the element?

Best Solution

You can use the onLoad event.

 <iframe onload="this.parentNode.removeChild(this);" src="/"></iframe>

Example here:

Or by using pure JavaScript:

var iframe = document.createElement("iframe");

iframe.src = "/";
iframe.onload = function() {


Example here: