【问题标题】:Is there a way to check whether the html is loaded on Iframe有没有办法检查 html 是否在 iframe 上加载
【发布时间】:2012-03-19 07:36:56
【问题描述】:

我的问题是我有两个 html 文件,例如 1.html 和 2.html。文件的内容是 1.html 它由 iframe 组成。 iframe 的来源是 2.html。 2.html 这是一个示例 html 页面。

我的问题是我想检查 2.html 是加载到 iframe 上还是直接加载到单独的浏览器上,而不是将其放在 iframe 中。检查只能从 2.html 开始。

任何建议的朋友。 提前致谢。

【问题讨论】:

  • 您可以在 iframe 上使用 onload 事件。但问题是onload 事件不会在IE 浏览器上触发。要检查文件的加载,您可以使用 ajax 请求

标签: javascript html iframe


【解决方案1】:

在 iframe 中加载时 window.parent 指向父窗口,但是在单独窗口中加载时 window.parent 指向窗口本身:

var loadinInIframe = window.parent != window;

【讨论】:

    【解决方案2】:

    如果使用 javascript,您可以使用 document.location 检查 window.parent.location 和 window.location,如果父位置不是您的应用程序位置,那么您可能是 iFramed

    【讨论】:

      【解决方案3】:

      在 iframe 的 onload 事件中绑定一个函数,并在父页面上设置一个加载的变量

      在 iframe 上

      window.onload = function() {
         parent.iframeLoaded = true;
      }
      

      在父页面上,只需声明变量来保存值

      var iframeLoaded = false;
      

      现在您可以在需要时从父页面检查此变量,例如

      if(iframeLoaded) {
          // Do something
      }
      

      【讨论】:

        【解决方案4】:
        if (top === self) { not in a frame } else { in a frame }
        

        Top 和 self 都是窗口对象(连同父窗口),因此您可以查看您的窗口是否是顶部窗口。

        归功于this。 希望对您有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-02-07
          • 1970-01-01
          • 1970-01-01
          • 2011-07-17
          • 1970-01-01
          • 2016-09-22
          • 1970-01-01
          相关资源
          最近更新 更多