在iframe完成加载后,加载指示符保留在Firefox中

时间:2013-08-21 09:08:53

标签: javascript html firefox load

我在页面的主体中放置了一个iframe:

<iframe src="about:blank" onload="this.contentWindow.document.write('some text')">

它运作良好,onload事件将iframe的内容替换为“some text”,就是这样。

但是在Firefox中它会导致旋转轮“加载指示器”,它会向您显示正在加载的页面永远不会消失。只有文本插入到iframe中,因此没有其他内容挂起,等待加载。当我从页面中删除iframe时,它会删除问题,所以我知道,这是导致它的iframe。

它在IE和Chrome中正常运行。

现在,我可以这样做:

 <iframe src="about:blank" onload="this.contentWindow.document.write('some text'); t.contentWindow.stop();">

这解决了Firefox中的问题,但它可以防止iframe中的任何图像加载(我不会在示例插入文本中使用任何图像,但稍后会添加它们。)

那么,我该如何解决这个问题?

2 个答案:

答案 0 :(得分:15)

请务必调用this.contentWindow.document.close();写完iframe之后。

答案 1 :(得分:1)

为什么首先使用同一iframe的load事件触发对contentWindow的写入?

另一种方法是创建一个iframe并将一些带有图像的内容加载到其中 - 按预期在Firefox上运行。没什么大不了的。

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function createIframe(){
    var f = document.getElementsByTagName('body') [0].appendChild(document.createElement('iframe'));
    f.contentWindow.document.write('<img src="http://www.funimage.org/uploads/posts/2013-02/1361892989_1_14.jpg" />');
}
</script>
</head>
<body onload="createIframe()">

</body>
</html>
相关问题