<html>
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
<title>如何使用 JavaScript 停止网页加载并等待 iframe 加载完成</title>
<body style="margin:0;padding:0;height:100%" scroll=no>
<iframe id="infrm" name="infrm" marginwidth="0" marginheight="0" width="100%" scrolling=auto src="http://fh.9a18.cn" frameborder="0" height="100%" onload="stopPageLoad()"></iframe>
<script>
function stopPageLoad() {
  // 获取iframe元素
  var iframe = document.getElementById("infrm");
  // 等待iframe加载完成
  if (iframe.contentWindow.document.readyState === "complete") {
    // 停止整个网页的加载
    window.stop();
  } else {
    // 继续等待iframe加载完成
    setTimeout(stopPageLoad, 100);
  }
}
</script>
</html>
<p>在 JavaScript 中,可以使用 window.onload 事件来确保整个网页加载完成后再执行某些操作。对于 iframe 的加载,可以使用 onload 事件来检测它是否已经加载完成。</p>
<p>上面的代码示例中,我们在 iframe 元素上添加了 onload 事件处理程序,以便检测 iframe 是否已经加载完成。在 stopPageLoad() 函数中,我们首先获取 iframe 元素,然后检查它的 readyState 属性是否为 'complete'。如果是,我们就调用 window.stop() 方法来停止整个网页的加载。否则,我们等待 100 毫秒后再次调用 stopPageLoad() 函数,直到 iframe 加载完成。</p>
如何使用 JavaScript 停止网页加载并等待 iframe 加载完成

原文地址: https://www.cveoy.top/t/topic/nMn5 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录