捕获iframe加载完成事件 [英] Capture iframe load complete event

查看:179
本文介绍了捕获iframe加载完成事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


解决方案

<$有一种方法可以捕获iframe的内容从父页面完全加载吗? c $ c>< iframe> 元素有一个 load 事件






您如何倾听该事件取决于您,但通常最好的方法是:

1)以编程方式创建iframe



它确保您的加载侦听器总是通过在iframe开始加载之前附加它来调用。

 < script> 
var iframe = document.createElement('iframe');
iframe.onload = function(){alert('myframe is loaded'); }; //在设置'src'之前
iframe.src ='...';
document.body.appendChild(iframe); //将其添加到文档中所需的任何位置
< / script>






2)内联javascript 是另一种可以在HTML标记中使用的方法。

 < script> 
函数onMyFrameLoad(){
alert('myframe is loaded');
};
< / script>

< iframe id =myframesrc =...onload =onMyFrameLoad(this)>< / iframe>






<3>您也可以附加事件侦听器<在元素
之后,位于< script> 标记内,但请记住,在这种情况下,iframe已经存在在你添加你的监听器的时候加载。因此有可能不会调用它(例如iframe非常快,或者来自缓存)。

  < iframe id =myframesrc =...>< / iframe> 

< script>
document.getElementById('myframe')。onload = function(){
alert('myframe is loaded');
};
< / script>






另请参阅我有关哪些元素也可以触发这种类型的加载事件


Is there a way to capture when the contents of an iframe have fully loaded from the parent page?

解决方案

<iframe> elements have a load event for that.


How you listen to that event is up to you, but generally the best way is to:

1) create your iframe programatically

It makes sure your load listener is always called by attaching it before the iframe starts loading.

<script>
var iframe = document.createElement('iframe');
iframe.onload = function() { alert('myframe is loaded'); }; // before setting 'src'
iframe.src = '...'; 
document.body.appendChild(iframe); // add it to wherever you need it in the document
</script>


2) inline javascript, is another way that you can use inside your HTML markup.

<script>
function onMyFrameLoad() {
  alert('myframe is loaded');
};
</script>

<iframe id="myframe" src="..." onload="onMyFrameLoad(this)"></iframe>


3) You may also attach the event listener after the element, inside a <script> tag, but keep in mind that in this case, there is a slight chance that the iframe is already loaded by the time you get to adding your listener. Therefore it's possible that it will not be called (e.g. if the iframe is very very fast, or coming from cache).

<iframe id="myframe" src="..."></iframe>

<script>
document.getElementById('myframe').onload = function() {
  alert('myframe is loaded');
};
</script>


Also see my other answer about which elements can also fire this type of load event

这篇关于捕获iframe加载完成事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆