Uros K
Uros K

Reputation: 3384

Check if iframe is loaded inside $(document).ready

I want to check if iframe is loaded with the following code:

$(document).ready(function() {
    jQuery('#iframeID').ready(somefunction);
}

It seems that 'somefunction' is called before iframe is loaded (the iframe is empty - just empty html-head-body).

Any idea why this happens?

Thank you.

Upvotes: 5

Views: 22563

Answers (3)

Jayram
Jayram

Reputation: 19588

I have tried:

$("#frameName").ready(function() {
    // Write you frame on load javascript code here
} );

and it did not work for me.

this did:

$("#frameName").load( function() {
     //code goes here
} );

Even though the event does not fire as quickly - it waits until images and css have loaded also.

Upvotes: 0

pandavenger
pandavenger

Reputation: 1027

This is because you're checking if the iFrame is ready, not the document inside.

$(document.getElementById('myframe').contentWindow.document).ready(someFunction);

should do the trick.

Upvotes: 1

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167220

Try this instead.

$('#iframeID').load(function() {
    callback(this);
});

While dealing with iFrames, it is good enough to use load() event instead of $(document).ready() event.

Upvotes: 15

Related Questions