捕获 iframe 加载完成事件
有没有办法捕获 iframe 的内容从父页面完全加载时的情况?
Is there a way to capture when the contents of an iframe have fully loaded from the parent page?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您还可以通过这种方式捕获 jquery 就绪事件:
这是一个工作示例:
http://jsfiddle.net/ZrFzF/< /a>
You can also capture jquery ready event this way:
Here is a working example:
http://jsfiddle.net/ZrFzF/
第1步:在模板中添加
iframe
。第2步:在Controller中添加负载监听器。
Step 1: Add
iframe
in template.Step 2: Add load listener in Controller.
请注意,如果在屏幕外加载 iframe,则 onload 事件似乎不会触发。使用“在新窗口中打开”/w 选项卡时经常会发生这种情况。
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.
在普通 JavaScript 中还有另一种一致的方法(仅适用于 IE9+):
如果您感兴趣在 Observables 中,这可以解决问题:
There is another consistent way (only for IE9+) in vanilla JavaScript for this:
And if you're interested in Observables this does the trick:
上面的答案都不适合我,但是这确实
更新:
正如@doppleganger在下面指出的那样,加载从jQuery 3.0开始就消失了,所以这里有一个使用
on。请注意,这实际上适用于 jQuery 1.7+,因此即使您尚未使用 jQuery 3.0,也可以通过这种方式实现它。
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.元素有一个
load
事件。如何监听该事件取决于您,但通常最好的方法是:
1) 以编程方式创建 iframe
它确保始终通过附加来调用您的
load
侦听器在 iframe 开始加载之前。2) 内联 javascript 是您可以在 HTML 标记中使用的另一种方法。
3) 您还可以在
标记内在元素之后附加事件侦听器,但请记住,在这种情况下,有可能会出现以下情况:当您添加侦听器时,iframe 已经加载。因此,它可能不会被调用(例如,如果iframe非常非常快,或者来自缓存)。
另请参阅我的其他答案,了解 哪些元素也可以触发此类
加载
事件<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