通过JavaScript将本地存储数据写入Cookie

| 我的任务是将浏览器本地存储中选定键的特定值复制到网站的Cookie中;使用的浏览器是Safari。 该特定值包含用户登录详细信息,因为设备上的浏览器只会被一个人使用,因此设计人员很乐意将这种敏感信息保存在浏览器的本地存储中。 我的问题是,如何使用Javascript向cookie中填充正确的数据?可能最初使用JSON存储数据。 谢谢你的时间。     
已邀请:
假设localStorage中的密钥为\'username \':
document.cookie=\'username=\' + localStorage.getItem(\'username\') + \'expires=\'+new Date((new Date().getTime()) + 1000*60*60*24*7).toGMTString() +\'; path=/\';
该Cookie将在7天后过期。将\'7 \'(那里只有一个)更改为您想要的天数。要存储其他键,只需将上面代码中两次出现的\'username \'更改为变量名(例如,\'password \')即可。     
您还可以使用jQuery Cookie插件
$.cookie(
    \'username\', 
    localStorage.getItem(\'username\'), { 
        expires: 7, 
        path: \'/\', 
        domain: \'jquery.com\', 
        secure: true 
});
    

要回复问题请先登录注册