Method for JS to judge whether iframe is loaded or not

  • 2021-07-09 06:40:52
  • OfStack

In this paper, an example is given to describe the method for JS to judge whether iframe is loaded or not. Share it for your reference, as follows:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
</head>
<body>
<script type="text/javascript">
var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
var iframe = document.createElement("iframe");
iframe.src = "https://www.ofstack.com/";
if (isIE) {
  iframe.onreadystatechange = function(){
    if(iframe.readyState == "loaded" || iframe.readyState == "complete"){
      alert("loaded");
    }
  };
} else {
  iframe.onload = function(){
    alert("loaded");
  };
}
document.body.appendChild(iframe);
</script>
</body>
</html>

Or:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
</head>
<body>
<script type="text/javascript">
var iframe = document.createElement("iframe");
iframe.src = "https://www.ofstack.com/";
if (iframe.attachEvent){
  iframe.attachEvent("onload", function(){
    alert("loaded");
  });
} else {
  iframe.onload = function(){
    alert("loaded");
  };
}
document.body.appendChild(iframe);
</script>
</body>
</html>

For more readers interested in JavaScript related content, please check the topics on this site: "Summary of JavaScript Operation iframe Skills", "Summary of JavaScript Search Algorithm Skills", "Summary of JavaScript Data Structure and Algorithm Skills", "Summary of JavaScript Traversal Algorithm and Skills", "Summary of json Operation Skills in JavaScript", "Summary of JavaScript Switching Effects and Skills", "Summary of JavaScript Animation Effects and Skills", "Summary of JavaScript Error and Debugging Skills" and "Summary of JavaScript Mathematical Operation Usage"

I hope this article is helpful to everyone's JavaScript programming.


Related articles: