store object in the SessionStorage  [ 763 views ]

Goal: to store object on the client side

The following code is simple not working:

var obj = {id: 1, name: 'cat'};
sessionStorage["pet"] = obj;
...
console.log(sessionStorage["pet"]);

the result is: [object Object]

We need to stringify the object before the store like this:

var obj = {id: 1, name: 'cat'};
sessionStorage["pet"] = JSON.stringify(obj);
...
console.log(JSON.parse(sessionStorage["pet"]));

now the result is: Object {id: 1, name: 'cat'}

and we are happy…

#sidebar a { color:#fff; } #sidebar ul ul li { color: #DEF585; } #sidebar h2 { color: #fff; } #sidebar ul p, #sidebar ul select { color: #BEDDBE; } #backfly { background: url(images/golfBallWallPaper.jpg) left bottom fixed repeat-x #65a51d; }