1. 程式人生 > >localStorage和sessionStorage

localStorage和sessionStorage

sessions tor log 永久 用法 utf oca 一個 style

localStorage和sessionStorage的區別:

localStorage(本地存儲)        大(5M)/cookie(4K)
  1.大:1280倍
  2.localStorage不會被發往服務器
  3.方便

  localStorage/sessionStorage
    localStorage      永久存儲
    sessionStorage    會話期存儲(瀏覽器關了就沒了)

  localStorage的用法跟json一模一樣
    localStorage.a=xxx            設置
    localStorage.a                讀取
    delete localStorage.a         刪掉
    for in                        遍歷

  sessionStorage用法和localStorage一模一樣

1.存一個對象進localStorage

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
    <script>
    let obj={
      name: blue,
      age: 18
    };

    localStorage.a=JSON.stringify(obj);

    let obj2=JSON.parse(localStorage.a);
    alert(obj2.name);
    
</script> </head> <body> </body> </html>

localStorage和sessionStorage