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

javascript

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: http://jsfiddle.net/mSZ4X/1/

Or by using pure JavaScript:

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

iframe.src = "/";
iframe.onload = function() {
    this.parentNode.removeChild(this);    
};

document.body.appendChild(iframe);

Example here: http://jsfiddle.net/mSZ4X/2/