Can sessionStorage only store strings?
PHP中文网
PHP中文网 2017-05-18 10:53:07
0
3
546

sessionStorage.setItem('xxx',true), get the result "true" string
sessionStorage.setItem('xxx',{}), get the result "[object Object]"
How Save other types?

PHP中文网
PHP中文网

认证0级讲师

reply all(3)
巴扎黑
sessionStorage.setItem('xxx',JSON.stringify({}));
迷茫

sessionStoragelocalStorage只能存字符串,针对对象需要存储storageIf you want, you can serialize it to achieve the effect:

var obj = {};
sessionStorage.setItem('xxx', JSON.stringify(obj)); //序列化
obj = JSON.parse(sessionStorage.setItem('xxx')); //这时候需要反序列化一下

Reference:

  • localStorage stores JSON objects

  • JS operation JSON

世界只因有你

webStorage itself can store non-strings, but no browser has implemented it yet

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template