【问题标题】:Possible to detect if a specific third-party cookie is allowed in the browser?是否可以检测浏览器中是否允许使用特定的第三方 cookie?
【发布时间】:2020-11-24 00:35:57
【问题描述】:

是否可以检测浏览器是否允许来自特定外部域的 cookie 被我网站的访问者读取?这个不受我控制的域显示在 iframe 中,但除非它有权访问其 cookie,否则它将无法工作。

启用第三方 Cookie 或将网站添加到设置中的“允许”部分后,一切正常。如果不是这种情况,我想显示一条用户友好的错误消息。

【问题讨论】:

标签: javascript cookies iframe cross-domain


【解决方案1】:

更好的方法是使用 poatMessageAPI 将 cookie 内容发送到 iframe。

这样你就不需要用户重新配置他们的浏览器来降低安全性

【讨论】:

  • 你能分享一个可以完成这个任务的sn-p代码吗?如果它来自第三方域,我如何获取此 cookie 的内容?另外,我虽然 postMessage 仅适用于 iframe -> 父窗口
【解决方案2】:

可以使用document.cookie 访问浏览器的cookie,使用iframe.contentDocument 访问iframe 的文档(其中iframe 是一个引用DOM 节点)。

有了它,应该能够检查(在 iframe 加载后)是否存在 cookie:

iframe.contentDocument.addEventListener("DOMContentLoaded", () => {
  if (iframe.contentDocument.cookie.split(';').find((item) => item.includes('cookie='))) {
    console.log("Cookie exists");
  }
}

【讨论】:

  • 这仅适用于 iframe 内容来自同一个域
猜你喜欢
  • 2013-02-03
  • 2022-01-17
  • 1970-01-01
  • 2014-02-14
  • 1970-01-01
  • 2011-04-02
  • 2021-01-10
  • 2020-02-20
  • 1970-01-01
相关资源
最近更新 更多