Home >Web Front-end >JS Tutorial >JS method to determine whether iframe is loaded

JS method to determine whether iframe is loaded

高洛峰
高洛峰Original
2017-01-12 11:03:041030browse

The example in this article describes the method of JS to determine whether the iframe is loaded. Share it with everyone 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 = "http://www.jb51.net/";
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 = "http://www.jb51.net/";
if (iframe.attachEvent){
  iframe.attachEvent("onload", function(){
    alert("loaded");
  });
} else {
  iframe.onload = function(){
    alert("loaded");
  };
}
document.body.appendChild(iframe);
</script>
</body>
</html>

I hope this article will be helpful to everyone in JavaScript programming.

For more articles related to the JS method of judging whether the iframe is loaded, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn