【问题标题】:how do i get url of iframe in page which contains iframe tag, when i navigate in iframe and url is changed?当我在 iframe 中导航并且 url 发生更改时,如何在包含 iframe 标记的页面中获取 iframe 的 url?
【发布时间】:2015-08-27 23:29:08
【问题描述】:

我有第一个域“mydomain.com”,我在其中放置了这个

<iframe src="yourdomain.com"></iframe>

我有第二个域“yourdomain.com”,其中包含网页并且位于 iframe src 中。 现在,当我在 iframe 中导航时,我想在“mydomain.com”中获取“yourdomain.com”的 url。 我已经测试过这段代码:

this.contentWindow.location

但它不起作用,它仅在两个文件在同一个域中时才起作用。 所以,如果有人知道如何做到这一点,请给我解决方案并帮助我。 提前致谢。

【问题讨论】:

  • 尝试与id 一起使用并尝试获取href document.getElementById("YourIframeId").contentDocument.location.href
  • 我也试过了,还是不行。

标签: javascript jquery html iframe cross-domain


【解决方案1】:

在 yourdomain.com 页面中试试这个:

window.parent.$('#iFrame').attr('src');

其中iFrame是iframe标签的id

【讨论】:

    【解决方案2】:

    试试这个

    window.top.location.src= "http://www.yourdomain.com"; 
    

    或使用此功能

    function getyourdomainUrl() {
    var isInIframe = (parent !== window),
        yourdomainUrl= null;
    
    if (isInIframe) {
        yourdomainUrl= document.referrer;
    }
    
    return yourdomainUrl;}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-02
      • 2014-03-29
      • 1970-01-01
      • 2011-12-29
      • 2015-02-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多