I have an iframe with id = "myIframe" and here my code to load it's content :
$('#myIframe').attr("src", "my_url");
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);
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(){})
.
Try this.
I had the same issue and added to this, i needed to check if iframe is loaded irrespective of cross-domain policy. I was developing a chrome extension which injects certain script on a webpage and displays some content from the parent page in an iframe. I tried following approach and this worked perfect for me.
P.S.: In my case, i do have control over content in iframe but not on the parent site. (Iframe is hosted on my own server)
First:
Create an iframe with a
data-
attribute in it like (this part was in injected script in my case)<iframe id="myiframe" src="http://anyurl.com" data-isloaded="0"></iframe>
Now in the iframe code, use :
Now back to the injected script as per my case:
and,
It may not exactly answer the question but it indeed is a possible case of this question which i solved by this method.
You can use the iframe's
load
event to respond when the iframe loads.This won't tell you whether the correct content loaded: To check that, you can inspect the
contentDocument
.