实时检测iframe页面上传

时间:2012-05-14 16:00:02

标签: javascript jquery dom iframe

我有一个包含一些iframe的div元素。有时,我会销毁那些iframe并创建新的iframe,但我总是需要知道他们每个人(也包括后来添加的)何时完成加载。我认为以下代码可以工作,但我没有记录到调试控制台。有什么帮助吗?

  // doesn't work
  $('#viewport').on('load', 'iframe', function() {
    console.log('LOADED!');
    return true;
  });

  // doesn't work either
  $('#viewport').on('ready', 'iframe', function() {
    console.log('LOADED!');
    return true;
  });

2 个答案:

答案 0 :(得分:0)

可能是在添加事件之前帧完成加载。

尝试在将事件添加到DOM之前添加事件。

var iframe = document.createElement("iframe");
// set iframe properties
$(iframe ).on('ready', 'iframe', function() {
  console.log('LOADED!');
});
document.body.appendChild(iframe);

答案 1 :(得分:0)

我非常确信动态iframe不能做到这一点。来自jQuery站点(在已弃用的load()函数下:

  

注意:.live()和.delegate()方法不能用于检测iframe的load事件。 load事件没有正确地冒泡父文档,而且事件委托所需的Firefox,IE9或Chrome都没有设置event.target。

我也无法使用类似的项目。