我想从 iFrame 中读取 window.top.location.origin
。
父级和 iFrame 位于不同的 HTTPS 域中。
例如,尝试访问会在 Chrome 中引发安全错误。
[DOMException:阻止来源为“...”的框架访问跨域框架。]
有没有可能在不触发错误的情况下做到这一点?
我需要 window.top
的来源,因为我根据该来源发送不同的 postMessages。
最佳答案
我知道这是旧的,但也许对其他人有帮助:
完整的父 URL 将出现在 <iframe/>
中作为document.referrer
.有了它,您可以在本地解析它以找到您可能需要的 URL 细节。
if (document.referrer !== location.href) {
let foo = document.createElement('a');
foo.href = document.referrer;
console.log('origin is:', foo.origin);
}
当然,这要归功于 anchor 标签的内置解析。隐藏的 gem ~!
关于javascript - 获取跨域 HTTPS iFrame 的父文档来源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26183875/