Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a javascript function when iframe finished loading?

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>
like image 992
user1788736 Avatar asked Feb 02 '13 23:02

user1788736


People also ask

How do you call a function while loading a page?

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.

How do I know if my iframe is finished loading?

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 .

Can iframe load JavaScript?

<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.

What is iframe onload?

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.


2 Answers

try this

<iframe id ='myframe' src='http://www.mysite.com/doit.php' onload="onLoadHandler();"></iframe>

<script type="text/javascript">
function onLoadHandler() {
    alert('loaded');
}
</script>
like image 117
pbaris Avatar answered Sep 22 '22 02:09

pbaris


Handle it just like anything loading:

$('#myframe').on('load', function() {
    // Handler for "load" called.
});

Obsolete answer:

$('#myframe').load(function() {
  // Handler for .load() called.
});
like image 29
John Conde Avatar answered Sep 22 '22 02:09

John Conde