【问题标题】:Read parent location from within a frame (different domains)从框架内读取父位置(不同域)
【发布时间】:2011-06-30 13:51:18
【问题描述】:

iframe 如何读取其父位置 URL,父窗口和 iframe 的域不同?

我知道这个问题的“常见”答案是由于域冲突,浏览器不允许跨域访问。 所以以下行不通

parent.location.href

但也许有人可以在这里开箱即用并提出类似于Cross-document messaging hack的建议?

【问题讨论】:

    标签: javascript dom iframe cross-domain


    【解决方案1】:

    也许你可以检查document.referrer?当然,这只有在用户尚未点击 iframe 中的链接时才有效。

    【讨论】:

    • 不,它不会从框架内返回任何内容。
    • @PleaseStand 这真的很有帮助。谢谢!
    【解决方案2】:

    我明白了!

    我的解决方案如下:

    1.在框架中使用 onload 属性

    <iframe src="http://website.com/" onload="src+='#'+document.location"></iframe>
    

    2。监控帧中的哈希变化

    var referrer = '?';
    window.onhashchange = function() {
      var ref = document.location.hash;
      if(ref && ref.length > 1) {
        var t = ref.split('#');
        ref = t[1];
      }
      referrer = ref;
    };
    

    还有“瞧”:-)

    顺便说一下,您会发现有关onhashchange 事件的更多详细信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多