Capture iframe load complete event
Solution 1
<iframe>
elements have a load
event for that.
How you listen to that event is up to you, but generally the best way is to:
1) create your iframe programatically
It makes sure your load
listener is always called by attaching it before the iframe starts loading.
<script>
var iframe = document.createElement('iframe');
iframe.onload = function() { alert('myframe is loaded'); }; // before setting 'src'
iframe.src = '...';
document.body.appendChild(iframe); // add it to wherever you need it in the document
</script>
2) inline javascript, is another way that you can use inside your HTML markup.
<script>
function onMyFrameLoad() {
alert('myframe is loaded');
};
</script>
<iframe id="myframe" src="..." onload="onMyFrameLoad(this)"></iframe>
3) You may also attach the event listener after the element, inside a <script>
tag, but keep in mind that in this case, there is a slight chance that the iframe is already loaded by the time you get to adding your listener. Therefore it's possible that it will not be called (e.g. if the iframe is very very fast, or coming from cache).
<iframe id="myframe" src="..."></iframe>
<script>
document.getElementById('myframe').onload = function() {
alert('myframe is loaded');
};
</script>
Also see my other answer about which elements can also fire this type of load
event
Solution 2
Neither of the above answers worked for me, however this did
UPDATE:
As @doppleganger pointed out below, load is gone as of jQuery 3.0, so here's an updated version that uses on
. Please note this will actually work on jQuery 1.7+, so you can implement it this way even if you're not on jQuery 3.0 yet.
$('iframe').on('load', function() {
// do stuff
});
Solution 3
There is another consistent way (only for IE9+) in vanilla JavaScript for this:
const iframe = document.getElementById('iframe');
const handleLoad = () => console.log('loaded');
iframe.addEventListener('load', handleLoad, true)
And if you're interested in Observables this does the trick:
import { fromEvent } from 'rxjs';
const iframe = document.getElementById('iframe');
fromEvent(iframe, 'load').subscribe(() => console.log('loaded');
Solution 4
Note that the onload event doesn't seem to fire if the iframe is loaded when offscreen. This frequently occurs when using "Open in New Window" /w tabs.
Solution 5
Step 1: Add iframe
in template.
<iframe id="uvIFrame" src="www.google.com"></iframe>
Step 2: Add load listener in Controller.
document.querySelector('iframe#uvIFrame').addEventListener('load', function () {
$scope.loading = false;
$scope.$apply();
});
Related videos on Youtube
Jaime Garcia
I am a software developer currently working on Java projects for BridgePhase LLC.
Updated on August 04, 2021Comments
-
Jaime Garcia almost 3 years
Is there a way to capture when the contents of an iframe have fully loaded from the parent page?
-
Pekka about 14 yearspossible duplicate of Reading Iframe Content in Iframe Load
-
-
Jaime Garcia almost 14 yearsThanks, this does exactly what I needed!
-
Stijn de Witt almost 10 yearsYou'll have to make an Ajax callback to your server. The shown code runs client-side in the browser.
-
roryok over 9 yearsI don't think that code does what you think it does. You can replace that "#iFrame" selector in your fiddle with anything and the alert still fires
-
Ivan Nikitin almost 9 yearsIn addition to @roryok, ready fires when DOM is ready, not the whole page loads.
-
Iest almost 9 yearsWith this method you can only have a single function run when the iframe is loaded. See my answer below using
addEventListener
which allows multiple callbacks to run on the load event. -
Iest almost 9 yearsThis approach is also vulnerable to a race condition as the iframe could load before the script tag is executed.
-
gblazex almost 9 yearsThe point of the answer is the
load
event. Now, you may use it in different ways. Of course you can useaddEventListener
, keep in mind this answer is from 2010 when it was safer to show the concept with theonload
property which worked in IE too (again, concept with the least amount of code). Your suggestion of the<script>
coming before theiframe
can fail becausegetElementById
is not guaranteed to find an element below thescript
tag. You may create the iframe programatically. That's the safest way to ensure your listener is added before the load event. -
Jerry almost 9 yearsThe load event doesn't work when you try to download a file.
-
gblazex almost 9 yearsexplain what you mean?
-
Doppelganger almost 8 yearsAs of jQuery 3.0,
load
is gone. Please use.on('load', function() { ... })
instead. -
Sergey Gussak about 7 yearsready is fired when the DOM is ready and js can be executed, not when the content is loaded.
-
Sergey Gussak about 7 yearsYes, that does not work in IE if the iFrame content is not HTML, e.g. PDF. See this: connect.microsoft.com/IE/feedback/details/809377/…
-
Popsyjunior about 7 years$("#iframeid").ready triggers the function when the element is in the dom. Not when the ifram has finished loading.
-
Raven over 6 yearsIf your using
jquery
orjqLite
then this is the way to go! -
Felipe N Moura over 5 yearsalso with iframes with display none ;)
-
Hossein Badrnezhad about 5 yearsit just called once :(
-
Sridhar Sarnobat about 5 yearsIs it a concern that my debugger stops at my breakpoint in
handleLoad()
before I see the iFrame render? I hope that's purely a rendering issue rather than a content loading issue. -
gumuruh over 4 yearshow about to capture that event by jquery when the iframe is already there... i.e : the iframe is not created by jquery.
-
Wisco crew over 4 yearsI ran into the race condition @Iest describes. My solution was to leave
src
andonload
blank in theiframe
tag, and then in javascript:document.getElementById('myframe').setAttribute('onload', '...'); document.getElementById('myframe').setAttribute('src', '...');
-
Mohammad Dehghan over 3 yearsI can't find the reference for the
load
event of theiframe
element. MDN only lists theload
event forWindow
andXMLHttpRequest
. Can anyone post a link to an authentic reference? I only found mentions of the event in W3C spec -
NitrusAphalion almost 3 yearsI have an iframe which is display: none but the onload event fires after posting a form to it.