Is there a way to capture when the contents of an iframe have fully loaded from the parent page?
相关问题
- Views base64 encoded blob in HTML with PHP
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
You can also capture jquery ready event this way:
Here is a working example:
http://jsfiddle.net/ZrFzF/
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.
There is another consistent way (only for IE9+) in vanilla JavaScript for this:
And if you're interested in Observables this does the trick:
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>
elements have aload
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.2) inline javascript, is another way that you can use inside your HTML markup.
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).Also see my other answer about which elements can also fire this type of
load
event