sessionStorage.setItem('xxx',true), get the result "true" string sessionStorage.setItem('xxx',{}), get the result "[object Object]"How Save other types?
认证0级讲师
sessionStorage.setItem('xxx',JSON.stringify({}));
sessionStorage和localStorage只能存字符串,针对对象需要存储storageIf you want, you can serialize it to achieve the effect:
sessionStorage
localStorage
storage
var obj = {}; sessionStorage.setItem('xxx', JSON.stringify(obj)); //序列化 obj = JSON.parse(sessionStorage.setItem('xxx')); //这时候需要反序列化一下
Reference: localStorage stores JSON objects JS operation JSON
Reference:
localStorage stores JSON objects
JS operation JSON
webStorage itself can store non-strings, but no browser has implemented it yet
sessionStorage
和localStorage
只能存字符串,针对对象需要存储storage
If you want, you can serialize it to achieve the effect:webStorage itself can store non-strings, but no browser has implemented it yet