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(){})
.
kindly use:
Updated my answer as the standards changed.
I'm not sure if you can detect whether it's loaded or not, but you can fire an event once it's done loading:
EDIT: As pointed out by Jesse Hallett, this will always fire when the
iframe
has loaded, even if it already has. So essentially, if theiframe
has already loaded, the callback will execute immediately.A really great method is use jQuery AJAX. The parent frame would look like this:
The iframe_load.php file would load the jQuery library and a JavaScript that attempts to load the destination URL in an AJAX GET:
IMPORTANT The destination must have contain the "Access-Control-Allow-Origin" header. Example in PHP:
in my case it was a cross-origin frame and wasn't loading sometimes. the solution that worked for me is: if it's loaded successfully then if you try this code:
it won't allow you to access
contentDocument
and throw a cross-origin error however if frame is not loaded successfully thencontentDocument
will return a#document
objectWhen an iFrame loads, it initially contains the #document, so checking the load state might best work by checking what's there now..
Easiest option: