[英]sessionStorage with document.domain
According to the spec, I should be able to set the document.domain to my root domain and thus have subdomains share sessionStorage. 根据规范,我应该能够将document.domain设置为我的根域,从而使子域共享sessionStorage。 But I can't seem to get that to work. 但是我似乎无法正常工作。 Is that no longer allowed? 不再允许了吗? https://html.spec.whatwg.org/multipage/browsers.html#relaxing-the-same-origin-restriction https://html.spec.whatwg.org/multipage/browsers.html#relaxing-the-same-origin-restriction
// set the document domain to the top level
let hostParts = location.hostname.split('.').reverse();
if (hostParts.length > 1)
document.domain = `${hostParts[1]}.${hostParts[0]}`;
this.sessionId = window.sessionStorage.getItem('hplrSn');
if (!this.sessionId) this.sessionId = generateId();
The above code will generate a new Id for both site1.dn.domain.com
and site2.dn.domain.com
上面的代码将为site1.dn.domain.com
和site2.dn.domain.com
生成一个新ID。
Check this question: In HTML5, is the localStorage object isolated per page/domain? 检查以下问题: 在HTML5中,是否按页面/域隔离了localStorage对象?
It work for page www.a.com/page1
and www.a.com/page2
, but not for your use case: sessionstorage for www.aacom
and www.bacom
is not shared. 它适用于www.a.com/page1
和www.a.com/page2
页面,但不适用于您的用例:不共享www.aacom
和www.bacom
。
You should try Cookie
您应该尝试Cookie
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.