我很高兴以前曾经问过这个问题,但是我一直在努力寻找适用于我的用例的最新答案:
利用cookie上的localStorage返回相当简单的“标志”值是否有性能/可支持性/简单性好处?
例如,我正在网站上实现用户可选的“暗模式”,并且当用户返回或在页面之间导航时,我不希望使用FOUC(又称“白色闪光”)。 Cookies现在是“旧技术”,但是将以下“ cookie代码”(当前基于jQuery)切换为localStorage是否有真正的好处?
public void setJasperPrint(JasperPrint jrPrint){
this.jrPrint = jrPrint;
//
jasperviewer = new JRViewer(jrPrint);
//Add the JRViewer object onto the container to render in GUI
c.add(jasperviewer);
然后,在打开jQuery(function() {
if (Cookies.get('darkmode') == '1') {
jQuery('body').addClass('dark-mode');
jQuery('.switch input').attr('checked', 'checked');
} else {
jQuery('body').removeClass('dark-mode');
jQuery('.switch input').attr('checked', false);
}
jQuery('.switch span').on('click', function() {
if (Cookies.get('darkmode') == '1') {
jQuery('body').removeClass('dark-mode');
Cookies.remove('darkmode', { path: '/' });
} else {
jQuery('body').addClass('dark-mode');
Cookies.set('darkmode', '1', { expires: 365, path: '/' });
}
});
});
标签之后,我便拥有了这个超级简单的脚本:
<body>
因此,总而言之-在这种使用localStorage的用例中有好处吗? Cookies目前可以正常工作,我已经否定了FOUC,但我仍然想知道利用localStorage进行性能,简单性处理等方面是否有实际好处?在整个GDPR争论中,我并没有真正大惊小怪,因为我的理解被平等地对待。