【问题标题】:how to detect ,on load of the html document, whether the body of the iframe document is ready to be displayed?如何在加载 html 文档时检测 iframe 文档的正文是否已准备好显示?
【发布时间】:2020-08-31 03:48:15
【问题描述】:

假设一个 HTML 文档有一个 iframe。 使用 javascript,我想在加载 html 文档时检测 iframe 文档的正文是否已准备好显示。

我希望能够覆盖 iframe 的正文内容(在实际加载之前)。 有什么建议么?我可以用 jquery 来做吗?

说如果,HTML 文档是

<html>
<head>
</head>
<body>
<iframe id="ifrmId"  src="http://www.google.com" >
</iframe>
</body>
</html>

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    您可以在 iframe 元素上使用 onload 事件:

    var iframe = document.getElementById('ifrmId');
    iframe.onload = function () {
      // iframe loaded
      var innerDocument = iframe.contentDocument || iframe.contentWindow.document;
    };
    

    编辑:用jQuery,一模一样的思路:

    $('#ifrmId').load(function () {
      // iframe loaded
      var innerDocument = $(this).contents();
    });
    

    请记住,如果要操作 iframe 的内部文档,文件需要在同一个域中,这是因为 same origin policy 的限制。

    【讨论】:

      猜你喜欢
      • 2013-01-09
      • 1970-01-01
      • 1970-01-01
      • 2011-10-12
      • 1970-01-01
      • 2010-11-01
      • 1970-01-01
      • 2012-06-11
      • 2015-07-12
      相关资源
      最近更新 更多