javascript - sessionStorage isn't working as expected -
here code:
sessionstorage.loggedin = true; if (sessionstorage.loggedin) { alert('true'); } else { alert('false'); }
simple enough. there must small thing i'm not understanding how javascript evaluating these expressions. when put sessionstorage.loggedin = false
, "false" alert shows correctly. however, when change sessionstorage.loggedin
true, "false" alert still pops, after clearing session. not getting right expression? seems simple, maybe need pair of eyes on it.
try change code to
sessionstorage.setitem('loggedin',json.stringify(true)); if (json.parse(sessionstorage.getitem('loggedin'))) { alert('true'); } else { alert('false'); }
and should work consistently across major browsers.
the interface setitem
/getitem
methods how spec written, going way safer using shortcut of assigning properties. also, sessionstorage
, localstorage
textbased storage mechanism, , not meant storing objects, need wrap calls json.parse
, json.stringify
expected results across board.
be aware json.parse
doesn't play nice undefined/null values, might wise type checking first.
Comments
Post a Comment