【问题标题】:Change window location href in subdomain iframe在子域 iframe 中更改窗口位置 href
【发布时间】:2021-11-17 22:12:57
【问题描述】:

我有a.example.comb.example.com

当我将 b.example.com/foo 作为 iframe 放入 a.example.com 时:

<iframe src='b.example.com/foo'></iframe>`

可从b.example.com 访问的window.location.href 值是...a.example.com/foo (??)...

问题是 - 我在b.example.com/foo 中使用了一个外部库来检查window.location.href,它得到的信息不正确。

对于这种情况,在我看来,我应该 iframe /foo 完全不同的域上的内容,例如: anotherexample.com/foo,其中window.location.href 实际上指向anotherexample.com/foo

如何强制 iframe 中的窗口对象实际使用其子域而不是添加参数的父域?

【问题讨论】:

    标签: javascript iframe cross-domain


    【解决方案1】:

    您确定在正确的子域中使用您的脚本吗?似乎您的外部库正在使用window.parent.location 访问父窗口,这是一个检查页面是直接加载还是在另一个页面中加载的代码:

    var is_loaded_directly = (window.location == window.parent.location);
    

    由于此代码有效,因此您的设置或库有问题。

    注意:也许你可以通过检查document.location而不是window.location来解决你的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-21
      • 1970-01-01
      • 2016-10-08
      • 2011-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多