捕获 iframe 加载完成事件

发布于 2024-09-07 17:14:45 字数 38 浏览 6 评论 0原文

有没有办法捕获 iframe 的内容从父页面完全加载时的情况?

Is there a way to capture when the contents of an iframe have fully loaded from the parent page?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

┈┾☆殇 2024-09-14 17:14:58

您还可以通过这种方式捕获 jquery 就绪事件:

$('#iframeid').ready(function () {
//Everything you need.
});

这是一个工作示例:

http://jsfiddle.net/ZrFzF/< /a>

You can also capture jquery ready event this way:

$('#iframeid').ready(function () {
//Everything you need.
});

Here is a working example:

http://jsfiddle.net/ZrFzF/

千鲤 2024-09-14 17:14:57

第1步:在模板中添加iframe

<iframe id="uvIFrame" src="www.google.com"></iframe>

第2步:在Controller中添加负载监听器。

document.querySelector('iframe#uvIFrame').addEventListener('load', function () {
  $scope.loading = false;
  $scope.$apply();
});

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();
});
≈。彩虹 2024-09-14 17:14:56

请注意,如果在屏幕外加载 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.

心如荒岛 2024-09-14 17:14:55

在普通 JavaScript 中还有另一种一致的方法(仅适用于 IE9+):

const iframe = document.getElementById('iframe');
const handleLoad = () => console.log('loaded');

iframe.addEventListener('load', handleLoad, true)

如果您感兴趣在 Observables 中,这可以解决问题:

import { fromEvent } from 'rxjs';

const iframe = document.getElementById('iframe');

fromEvent(iframe, 'load').subscribe(() => console.log('loaded'));

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'));
凉栀 2024-09-14 17:14:53

上面的答案都不适合我,但是这确实

更新

正如@doppleganger在下面指出的那样,加载从jQuery 3.0开始就消失了,所以这里有一个使用on。请注意,这实际上适用于 jQuery 1.7+,因此即使您尚未使用 jQuery 3.0,也可以通过这种方式实现它。

$('iframe').on('load', function() {
    // do stuff 
});

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 
});
邮友 2024-09-14 17:14:52

元素有一个 load 事件


如何监听该事件取决于您,但通常最好的方法是:

1) 以编程方式创建 iframe

它确保始终通过附加来调用您的 load 侦听器 iframe 开始加载之前。

<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) 内联 javascript 是您可以在 HTML 标记中使用的另一种方法。

<script>
function onMyFrameLoad() {
  alert('myframe is loaded');
};
</script>

<iframe id="myframe" src="..." onload="onMyFrameLoad(this)"></iframe>

3) 您还可以在

<iframe id="myframe" src="..."></iframe>

<script>
document.getElementById('myframe').onload = function() {
  alert('myframe is loaded');
};
</script>

另请参阅我的其他答案,了解 哪些元素也可以触发此类 加载事件

<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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文