Could any one tell me how I can detect if an iframe has finished loading so I can call a javascript function and alert the user that iframe finished loading and do some other process inside the javascript function? (Note my iframe is my own site) Can I fire callback from within the iframe as I can have control over it? if yes how ?
<iframe id ='myframe' src='http://www.example.com/doit.php'></iframe>
The first approach for calling a function on the page load is the use an onload event inside the HTML <body> tag. As you know, the HTML body contains the entire content of the web page, and when all HTML body loads on the web browser, it will call the function from the JavaScript.
If you're hosting the page and the iframe on the same domain you can listen for the iframe's Window. DOMContentLoaded event. You have to wait for the original page to fire DOMContentLoaded first, then attach a DOMContentLoaded event listener on the iframe's Window .
<iframe> elements have a load event for that. It makes sure your load listener is always called by attaching it before the iframe starts loading. 2) inline javascript, is another way that you can use inside your HTML markup.
2 months ago. by Shehroz Azam. The onload event is used to run scripts and call JavaScript functions after the contents of a webpage have been loaded. It can be used to get the information about the user's browser so that the compatible version of the web page can be loaded.
try this
<iframe id ='myframe' src='http://www.mysite.com/doit.php' onload="onLoadHandler();"></iframe>
<script type="text/javascript">
function onLoadHandler() {
alert('loaded');
}
</script>
Handle it just like anything loading:
$('#myframe').on('load', function() {
// Handler for "load" called.
});
Obsolete answer:
$('#myframe').load(function() {
// Handler for .load() called.
});
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