How to capture iframe load complete event with JavaScript?

Spread the love

Sometimes, we want to capture iframe load complete event with JavaScript.

In this article, we’ll look at how to capture iframe load complete event with JavaScript.

How to capture iframe load complete event with JavaScript?

To capture iframe load complete event with JavaScript, we set its onload property to the event listener.

For instance, we write

const iframe = document.createElement("iframe");
iframe.onload = () => {
  console.log("myframe is loaded");
};
iframe.src = "...";
document.body.appendChild(iframe);

to select the iframe with createElement.

Then we set its onload property to a function that’s called when the iframe’s contents is loaded.

Next, we set the src property to the iframe content’s URL.

Then we call appendchild to append the iframe to the body.

Conclusion

To capture iframe load complete event with JavaScript, we set its onload property to the event listener.

Leave a Reply

Your email address will not be published. Required fields are marked *