现象:我的项目是用iframe内嵌到别人的网站里的,谷歌浏览器页面空白,控制台报错
当使用chrome 无痕模式时, 默认阻止第三方cookie
如果使用了localStorge或者cookie , 就会在js里报错
Uncaught DOMException: Failed to read the 'localStorage' property
from 'Window': Access is denied for this document.
方法一:
在Chrome设置菜单中: 高级 > 隐私设置和安全性 > 内容设置 > Cookie > 阻止第三方Cookie > 将这项关闭
方法二:
代码设置白名单,白名单以外的页面需要登录设置cookie,白名单不需要设置cookie,这是只针对iframe内嵌的第一个页面,不需要cookie值,把这个页面设置在白名单里,可以解决无痕报错问题
但是如果内嵌的第一个页面确实需要来自其他网站的cookie,目前只能通过方法一
//他人的网站
<iframe src="我的网址/test" class="bottom-iframe"></iframe>
//我的项目代码router/index.js
const WHITE_LIST = [ 'test']
router.beforeEach((to, from, next) => {
if (WHITE_LIST.includes(to.name)) {
// 白名单不需要token
} else {
const token = getToken()
if (!token) {
// 无token的处理
} else {
// 有token的处理
}
}
})