sessionStorage与document.domain

时间:2017-03-18 21:22:58

标签: javascript

根据规范,我应该能够将document.domain设置为我的根域,从而让子域共享sessionStorage。但我似乎无法让它发挥作用。这不再允许吗? 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();

以上代码会为site1.dn.domain.comsite2.dn.domain.com

生成新ID

1 个答案:

答案 0 :(得分:0)

请检查此问题:In HTML5, is the localStorage object isolated per page/domain?

适用于页面www.a.com/page1www.a.com/page2,但不适用于您的用例:www.a.a.comwww.b.a.com的sessionstorage未共享。

您应该尝试Cookie