【问题标题】:find out what page the iframe is on找出 iframe 在哪个页面上
【发布时间】:2009-11-18 10:34:22
【问题描述】:

我的网站顶部有一个“工具栏”,页面内容是 iframe。 如何使用 javascript 找出 iframe 的当前 URL 是什么?

【问题讨论】:

    标签: javascript iframe


    【解决方案1】:

    如果 iframe 位于不同的域中,或者违反了same origin policy,这可能是不可能的。例如,如果页面位于 example.com/foo,而 iframe 位于 example.org/bar,则无法获取位置。

    如果你没有违反同源政策,你可以使用这样的:

    window.frames["iframeID"].location.href
    

    【讨论】:

      【解决方案2】:

      检查“src”属性。

      例如 window.frames["your_iframe"].src

      【讨论】:

      • 这将返回 src 位置。如果用户从原始页面位置导航怎么办。我需要页面所在的实际位置..!
      • 是的,然后您需要检查文档的 location.href。
      【解决方案3】:
      function getUrl()
      {
          var pageFrame = $find("frame_id_goes_here", document);
          if(pageFrame) {
              return pageFrame.location;
          }
      }
      

      【讨论】:

        【解决方案4】:

        window.frames[0].location.href 怎么样?

        或者如果你知道 iframe 的 id,那么 document.getElementById("iframe1").src 应该可以工作。

        【讨论】:

          【解决方案5】:

          假设 iframe 是页面上的第一个:

          document.getElementsByTagName('IFRAME')[0].contentDocument.location.href
          

          当然,您总是可以通过 ID 获取它:

          document.getElementById('myIframe').contentDocument.location.href
          

          contentDocument 是关键。

          祝你好运!

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-02-08
            • 2011-06-07
            • 2020-09-05
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多