本文介绍了如何检查iframe是否已加载或是否有内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个id =myIframe的iframe,这里我的代码加载它的内容:
I have an iframe with id = "myIframe" and here my code to load it's content :
$('#myIframe').attr("src", "my_url");
问题有时需要很长时间才能加载,有时加载速度非常快。所以我必须使用setTimeout函数:
The problem is sometimes it take too long for loading and sometimes it loaded very quickly. So I must to use "setTimeout" function :
setTimeout(function(){
if (//something shows iframe is loaded or has content)
{
//my code
}
else
{
$('#myIframe').attr("src",""); //stop loading content
}
},5000);
我想知道的是如何确定iFrame是否已加载或是否有内容。使用 iframe.contents()。find()
将无效。我不能使用 iframe.load(function(){})
。
All I want to know is how to find out if an iFrame is loaded or it has content. Using iframe.contents().find()
will not work. I can't use iframe.load(function(){})
.
推荐答案
试试这个。
<script>
function checkIframeLoaded() {
// Get a handle to the iframe element
var iframe = document.getElementById('i_frame');
var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
// Check if loading is complete
if ( iframeDoc.readyState == 'complete' ) {
//iframe.contentWindow.alert("Hello");
iframe.contentWindow.onload = function(){
alert("I am loaded");
};
// The loading is complete, call the function we want executed once the iframe is loaded
afterLoading();
return;
}
// If we are here, it is not loaded. Set things up so we check the status again in 100 milliseconds
window.setTimeout(checkIframeLoaded, 100);
}
function afterLoading(){
alert("I am here");
}
</script>
<body onload="checkIframeLoaded();">
这篇关于如何检查iframe是否已加载或是否有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!