Say I want to bind a load-event to an iframe:
var callback = function() {
//Loaded
};
$('iframe').one('load', callback);
That works well enough, except I can not be sure that the iframe is not already loaded. In that case, the load-event has already fired and my function will never run.
How do I check if the iframe already is loaded?
var callback = function() {
//Loaded
};
if(iframe already loaded) {
callback();
} else {
$('iframe').one('load', callback);
}
You can check readyState
:
var iframe = $('iframe');
if (iframe.prop('contentWindow') && iframe.prop('contentWindow').document.readyState === 'complete') {
callback();
} else {
iframe.one('load', callback);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With