【问题标题】:Is it possible to block cookies within an iframe?是否可以在 iframe 中阻止 cookie?
【发布时间】:2019-12-12 06:00:00
【问题描述】:

是否可以强制 iframe 在没有 cookie 的情况下加载并为用户创建一个全新的会话?

例如:我是否可以在网站上包含 <iframe src="https://stackoverflow.com">,这样即使当前登录的 StackOverflow 用户也不会自动登录?

“隐身模式”iframe,如果你愿意的话。

【问题讨论】:

    标签: html iframe


    【解决方案1】:

    HTML5 沙盒属性(没有 allow-same-origin 关键字)可防止 iframe 读取/写入 cookie。这对于同源和跨源 iframe 都是如此。

    allow-scripts 属性会启用 JS,但不会干扰对 cookie 的限制。

    <iframe sandbox="allow-scripts" src="..."></iframe>

    -通过 Chava G Recommended method to prevent any content inside iframe from setting cookies

    【讨论】:

      【解决方案2】:

      如果您想要 iframe,请不要设置 cookie 并正确呈现:

      <iframe sandbox="allow-scripts" src="..."></iframe>
      

      如果你只想不显示 iframe:

        <iframe id="FR" src="">
      

      在java脚本中:

      document.getElementById("FR").style.display = "none";
      document.getElementById("FR").style.visibility = "hidden";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-20
        • 2012-05-05
        • 2020-04-30
        • 1970-01-01
        • 1970-01-01
        • 2020-04-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多