繁体   English   中英

获取跨域HTTPS iFrame的父文档来源

[英]Get parent document origin for cross-domain HTTPS iFrame

我想从iFrame中读取window.top.location.origin
父级和iFrame位于不同的HTTPS域上。
例如,尝试访问会引发安全错误的Chrome。
[DOMException: Blocked a frame with origin "..." from accessing a cross-origin frame.]

是否有可能在不触发错误的情况下做到这一点?

我需要window.top的来源,因为我基于该来源发送了不同的postMessages。

我知道这很老了,但也许对其他人有帮助:

完整的父URL将以document.referrer出现在<iframe/>中。 这样,您可以在本地对其进行解析以找到您可能需要的URL详细信息。

if (document.referrer !== location.href) {
  let foo = document.createElement('a');
  foo.href = document.referrer;
  console.log('origin is:', foo.origin);
}

当然,这要归功于anchor标签的内置解析。 隐藏的宝石〜!

由于同源政策,来自其他来源的iframe中的JavaScript将无法与其父框架进行通信。 如果您有权访问为iframe服务的服务器,则可以启用CORS,否则我认为您不走运

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM