【问题标题】:Get current URL from IFRAME从 IFRAME 获取当前 URL
【发布时间】:2010-10-30 14:32:06
【问题描述】:

有没有从 iframe 获取当前 URL 的简单方法?

观看者会浏览多个网站。 我猜我会在 javascript 中使用一些东西。

【问题讨论】:

标签: javascript url iframe


【解决方案1】:

出于安全原因,您只能在 iframe 的内容和引用的 javascript 来自同一域的情况下获取 url。只要这是真的,这样的事情就会起作用:

document.getElementById("iframe_id").contentWindow.location.href

如果两个域不匹配,您将遇到跨站点引用脚本安全限制。

另见answers to a similar question

【讨论】:

  • 在我链接的问题中没有成功的答案...如果您的意思是最重要的答案,它并不是一个真正有效的解决方案。使用 IE,您可以尝试上述问题的答案中提供的 HTA 方法。
  • 如果我们将属性sandbox="allow-same-origin"添加到iFrame中会怎样?
  • 如果我们在 iframe 的目标服务器上允许新源,我们会遇到问题吗?
  • 另一种方式:window.frames[0].location.href
【解决方案2】:

如果您的 iframe 来自另一个域(跨域),那么其他答案对您没有帮助...您只需要使用这个:

var currentUrl = document.referrer;

而且 - 这里有主网址!

【讨论】:

  • -1 OP 正在寻找 iframe 的当前 url,而不是父页面的引用。
  • @Christophe - 我知道,但它可以帮助人们寻找获取主网址并提出这个问题!
  • 这不会给你最多(尝试使用 2 个嵌套的 iframe),但即使只考虑一个级别,也很容易伪造它,因此你不能完全依赖它!
  • 阿斯洛:document.getElementById("iframe").contentDocument.referrer
  • 你救了我的命,这正是我所需要的
【解决方案3】:

希望这对您的情况有所帮助, 我遇到了完全相同的问题,只是使用 localstorage 在父窗口和 iframe 之间共享数据。 所以在父窗口中你可以:

localStorage.setItem("url", myUrl);

而在 iframe 源代码中只是从本地存储中获取这些数据:

localStorage.getItem('url');

为我节省了很多时间。 据我所知,唯一的条件是访问父页面代码。 希望这会对某人有所帮助。

【讨论】:

  • 本地存储只能在同一个域上运行......那么为什么要解决所有这些麻烦呢?只需查询 location.href
【解决方案4】:

如果你在 iframe 上下文中,

你可以的

const currentIframeHref = new URL(document.location.href);
const urlOrigin = currentIframeHref.origin;
const urlFilePath = decodeURIComponent(currentIframeHref.pathname);

如果您在父窗口/框架中,那么您可以使用https://stackoverflow.com/a/938195/2305243 的答案,即

document.getElementById("iframe_id").contentWindow.location.href

【讨论】:

    【解决方案5】:

    我遇到了 blob url href 的问题。因此,通过引用 iframe,我刚刚从 iframe 的 src 属性中生成了一个 url:

        const iframeReference = document.getElementById("iframe_id");
        const iframeUrl = iframeReference ? new URL(iframeReference.src) : undefined;
        if (iframeUrl) {
            console.log("Voila: " + iframeUrl);
        } else {
            console.warn("iframe with id iframe_id not found");
        }
    

    【讨论】:

      【解决方案6】:

      为可能遇到此问题的任何人提供一些额外信息:

      如果您在加载之前尝试从 iframe 获取 URL,您将获得空值。 我通过在 javascript 中创建整个 iframe 并使用 onLoad 函数获取我需要的值来解决这个问题:

      var iframe = document.createElement('iframe');
      iframe.onload = function() {
      
          //some custom settings
          this.width=screen.width;this.height=screen.height; this.passing=0; this.frameBorder="0";
      
          var href = iframe.contentWindow.location.href;
          var origin = iframe.contentWindow.location.origin;
          var url = iframe.contentWindow.location.url;
          var path = iframe.contentWindow.location.pathname;
      
          console.log("href: ", href)
          console.log("origin: ", origin)
          console.log("path: ", path)
          console.log("url: ", url)
      };
      
      iframe.src = 'http://localhost/folder/index.html';
      
      document.body.appendChild(iframe);
      

      由于同源策略,我在访问“跨源”帧时遇到了问题 - 我通过在本地运行网络服务器而不是直接从我的磁盘运行所有文件来解决这个问题。为了使所有这些工作,您需要使用与源相同的协议、主机名和端口来访问 iframe。从我的磁盘运行所有文件时,不确定其中哪些丢失/丢失了。

      另外,关于位置对象的更多信息:https://www.w3schools.com/JSREF/obj_location.asp

      【讨论】:

        【解决方案7】:

        如果您在没有跨域 src 的 iframe 中,或者 src 为空:

        然后:

        function getOriginUrl() {
            var href = document.location.href;
            var referrer = document.referrer;
            // Check if window.frameElement not null
            if(window.frameElement) {
                href = window.frameElement.ownerDocument.location.href;
                // This one will be origin
                if(window.frameElement.ownerDocument.referrer != "") {
                    referrer = window.frameElement.ownerDocument.referrer;
                }
            }
            // Compare if href not equal to referrer
            if(href != referrer) {
                // Take referrer as origin
                return referrer;
            } else {
                // Take href
                return href
            }
        }
        

        如果您在具有跨域 src 的 iframe 中:

        然后:

        function getOriginUrl() {
            var href = document.location.href;
            var referrer = document.referrer;
            // Detect if you're inside an iframe
            if(window.parent != window) {
                // Take referrer as origin
                return referrer;
            } else {
                // Take href
                return href;
            }
        }
        

        【讨论】:

          【解决方案8】:

          我一直在尝试从 shopify 谷歌分析附加脚本 iframe 内部检索完整的 URL。 document.refferer 仅显示没有搜索参数的主机名。但我找到了另一个适合我的属性document.baseURI

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-11-12
            • 1970-01-01
            • 2012-07-05
            • 2012-06-12
            • 2012-06-19
            相关资源
            最近更新 更多