简体   繁体   English

如何在执行函数之前等待iframe刷新?

[英]How to wait until iframe refresh is done before executing function?

I am refreshing an iframe on my page when someone clicks on an image with this code: 当有人点击带有此代码的图片时,我正在刷新页面上的iframe:

 $("img").click(function() {
      $('#space iframe').attr('src', function(i, val){ return val; });

      openBox();
 });

But I only want to execute openBox() function after the iframe is done refreshing. 但我只想在iframe完成刷新后执行openBox()函数。

How can I achieve something like that? 我怎样才能实现这样的目标?

Using jQuery: 使用jQuery:

$('#frameId').on('load', function() {
  //code to execute after frame loaded
});

Using vanilla JavaScript: 使用vanilla JavaScript:

document.getElementById('frameId').addEventListener('load', function() {
  //code to execute after frame loaded
});

While this isn't exactly your problem, here is how you listen for an load event on an iFrame . 虽然这不是您的问题,但这里是您在iFrame上监听加载事件的方式。

HTML HTML

<iframe id="frame"></iframe>
<button id="loadFrame">load frame</button>

JS JS

$("#loadFrame").click(function () {
    $("#frame").attr("src", "http://www.google.com");
    $("#frame").on("load", function () {
        alert("ad");
    });    
 });

Fiddle: http://jsfiddle.net/7RL35/4/ 小提琴: http//jsfiddle.net/7RL35/4/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM