首页 > 解决方案 > 在子域 iframe 中更改窗口位置 href

问题描述

我有a.example.comb.example.com;

当我将b.example.com/fooiframe 放入a.example.com

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

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

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

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

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

标签: javascriptiframecross-domain

解决方案


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

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

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

注意:也许您可以通过检查document.location而不是window.location.


推荐阅读