How to capture iframe load complete event with JavaScript?

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 can set the onload property of the iframe to a function that runs when the iframe content finishes loading.

For instance, we write

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

to select the iframe with createElement.

Then we set its onload property to a function that runs when the iframe content is loaded.

And then we set the src property of the iframe to load it.

Conclusion

To capture iframe load complete event with JavaScript, we can set the onload property of the iframe to a function that runs when the iframe content finishes loading.