即使未在Safari上加载iframe,也会在加载时触发iframe [英] Iframe on load triggering even if iframe is not loaded on Safari

查看:208
本文介绍了即使未在Safari上加载iframe,也会在加载时触发iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Google文档查看器加载文档的iframe时遇到问题,有时文档无法加载.

I had problem with iframe that was loading a document using google document viewer, sometimes the document would not load.

我使用了解决方案来检查iframe是否成功加载或是否应该重新加载.

I used this solution to check if iframe is successfully loaded, or if it should be reloaded.

该解决方案在pc(chrome)上效果很好,但在iphone(safari)$('#iframe').on('load',...)事件上触发,即使没有内容也是如此.

The solution works great on pc(chrome), but on iphone(safari) $('#iframe').on('load', ...) event triggers even when there is no content.

仅在iframe真正加载并包含内容时,才会触发在PC上加载"事件.

On pc on load event is only triggered when iframe is trully loaded and has content.

 function reloadIFrame() {
    document.getElementById("ifm").src=document.getElementById("ifm").src;
 }
 ints.push( setInterval("reloadIFrame();", 2000));

  $( document ).ready(function() {
      $('#ifm').on('load', function() {
          // this event fires even when there is no content on safari
          // ....stop reloading the iframe etc.....
          alert($('#ifm').attr('src')); // this gives me correct url, even when not loaded

iframe:

<iframe data-iframe="true" id="ifm" data-ext="<?php echo $ext ?>" 
  class="document-iframe"  
  src="<?php echo $full_src ?>" data-tool-id="<?php echo $tool->ID ?>">
 </iframe>

推荐答案

如果与任何人相关,请使用iframe元素contentWindow.length解决Safari问题.您可以在此处获得更多信息: HTMLIframeElement

If relevant to anyone, resolved the Safari issue using the iframe element contentWindow.length. you can get more info here: HTMLIframeElement

在Safari上,无论如何都会激活onLoad,因此onLoad函数可确保内容已实际加载或需要重新加载

When on Safari the onLoad is activated in any case so the onLoad function makes sure content was actually loaded or needs to reload

这是我的解决方案,基于此处

This is my solution based on the original solution posted here

iframeLoaded() {
 const contentLoaded = this.refs.iframe.contentWindow.length

 if (!!contentLoaded) {
   clearInterval(this.iframeTimeoutId)
 }
 else {
   this.updateIframeSrc()
 }
}

这篇关于即使未在Safari上加载iframe,也会在加载时触发iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆