ストレージを学ぶ localStorageとsessionStorage、setItemメソッドとgetItemメソッド
ハンズオンJavaScript (あんどうやすし(著)、オライリー・ジャパン)の14章(ストレージを学ぶ)、14.5(練習問題)の14-2、14-3の解答を求めてみる。
入出力結果(Safari、Develop menu, Console)
[Error] Failed to load resource: the server responded with a status of 404 () (favicon.ico, line 0)
> localStorage
< Storage {length: 0, key: function, getItem: function, setItem: function, removeItem: function, …}
> sessionStorage
< Storage {length: 0, key: function, getItem: function, setItem: function, removeItem: function, …}
> localStorage.setItem('a', 1)
< undefined
> localStorage
< Storage {a: "1", length: 1, key: function, getItem: function, setItem: function, …}
> sessionStorage.setItem('b', '2')
< undefined
> sessionStorage
< Storage {b: "2", length: 1, key: function, getItem: function, setItem: function, …}
タブを閉じて新規タブで開き直した後
入出力結果(Safari、Develop menu, Console)
> localStorage
< Storage {a: "1", length: 1, key: function, getItem: function, setItem: function, …}
> sessionStorage
< Storage {length: 0, key: function, getItem: function, setItem: function, removeItem: function, …}
> localStorage.getItem('a')
< "1"
> sessionStorage.getItem('b')
< null