JavaScript在加载Iframe时显示加载gif

JavaScript在加载Iframe时显示加载gif,javascript,iframe,loading,Javascript,Iframe,Loading,我需要在加载iframe时显示加载GIF。 这就是我想到的: <div id="m-load" style="position:relative;top:45%;left:45%;"> <img src="images/loader.gif"/></div> <iframe src="http://myserver.com/myfile.html" id="m-iframe"></iframe> 在我输入的iframe源文件中:(

我需要在加载iframe时显示加载GIF。
这就是我想到的:

<div id="m-load" style="position:relative;top:45%;left:45%;">
<img src="images/loader.gif"/></div>
<iframe src="http://myserver.com/myfile.html" id="m-iframe"></iframe>

在我输入的iframe源文件中:(myfile.html)


top.document.getElementById('m-load').style.display=“无”;
但这在Firefox中不起作用(权限被拒绝)。

实现此目的的任何其他方法?

您需要在相同的页面上使用
onload
事件执行此操作,该事件在所有iFrame加载后按如下方式触发:

// show loading image

window.onload = function(){
  // hide loading image
}


window.onload=函数(){
document.getElementById('img').style.display=“无”;
}

iFrame
不允许随意访问父内容。假设一个黑客在你的页面中发现了一个漏洞(比如,你有一个评论表单,允许
iframe
s用于任何目的。如果他们可以访问父网站,他们就可以在你的网站上到处走动

您可以改为使用
postMessage
。因为页面和iframe现在都同意如何通信,以及什么是有效通信,所以您不能随意访问页面。在

// show loading image

window.onload = function(){
  // hide loading image
}
<img id="img" src="images/loader.gif"/></div>

<script>
window.onload = function(){
   document.getElementById('img').style.display = "none";
}
</script>