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

Fra*_*isc 1 javascript iframe postmessage cross-domain

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

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

我需要window.top的来源,因为我根据该来源发送不同的 postMessage。

luk*_*eed 6

我知道这很旧,但也许它可以帮助其他人:

完整的父 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);
}
Run Code Online (Sandbox Code Playgroud)

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