'JavaScript wait until content loads in iframe and then load another content

I want to create iframe when page is loaded, load content#1 and wait until it loads in the iframe and then load another content#2 the same iframe, then wait some interval and remove iframe.

Also setTimeout functions will be complex and should return some status variables...

Do I do it proper way?

Sorry, I am not good in JavaScript, what happens if loading of content#1 takes more than 25s. i.e. when iframe is removed.

Any pro example/idea? Thanks!

window.onload = function() {

var iframe = document.createElement('iframe');
iframe.id = "iframeid";
iframe.src = 'data:text/html,' + 'content#1';

document.body.appendChild(iframe);

setTimeout(function(){document.body.removeChild(iframe);},25000);
setTimeout(function(){iframe.src = 'data:text/html,' + 'content#2';},5000);

}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source