Iframe Onload Event

If you want to run JavaScript when an iframe has finished loading, you can either:

  • include code in the iframe tag's onload attribute, or
  • assign code to the iframe's onload event handler property.

The following demonstrates calling a function from the iframe's onload attribute:

<iframe id="ifrm" src="demo.html" onload="doSomething()"></iframe>

This example demonstrates assigning a function to an iframe's onload event handler property:

document.getElementById('ifrm').onload = function() {
    // put your awesome code here
}

Another option: you can include an onload handler in the iframed document itself using its window.onload property or body onload attribute. All browsers in current use support all these approaches.

Note: Onload events are fired on iframe elements before they are fired on the containing document. See iframe onload in use in Access to Iframe and Contents.