19

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>

3 Answers 3

50

try this

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

<script type="text/javascript">
function onLoadHandler() {
    alert('loaded');
}
</script>
Sign up to request clarification or add additional context in comments.

Comments

14

If using jQuery, handle it just like anything loading:

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

Obsolete answer:

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

2 Comments

Calling .load is now deprecated, use .on('load', function() {}) instead
Only add a note: It is deprecated since version 3.x, but you can use it yet if you are in older versions
3

With just plain javascript

document.querySelector('#myframe').addEventListener("load", ev => {
    // your stuff
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.