Session-level local storage: sessionStorage

A Js object is added in HTML5: sessionStorage; through this object, the session-level WebStorage stored in the browser can be directly operated. The data stored in sessionStorage is first in the form of Key-Value. In addition, it is related to the current session of the browser. When the session ends, the data will be automatically cleared, similar to a cookie with no expiration time set.

  • sessionStorage provides four methods to assist us in performing related operations on local storage.

  • setItem(key,value) adds local storage data. The two parameters are very simple and I won’t go into details.

  • getItem(key) gets the corresponding Value through key.

  • removeItem(key) deletes local data by key.

  • clear() clears the data.

The code is as follows:

                          
    <script type="text/javascript">
        //添加key-value 数据到 sessionStorage
        localStorage.setItem("demokey", "http://blog.itjeek.com");
        //通过key来获取value
        var dt = localStorage.getItem("demokey");
        alert(dt);
        //清空所有的key-value数据。
        //localStorage.clear();
        alert(localStorage.length);
    </script>


Continuing Learning
||
<!DOCTYPE html> <html> <head>  <meta charset="utf-8">  <title>php中文网</title>  </head> <body> <script type="text/javascript"> //添加key-value 数据到 sessionStorage localStorage.setItem("demokey", "http://blog.itjeek.com"); //通过key来获取value var dt = localStorage.getItem("demokey"); alert(dt); //清空所有的key-value数据。 //localStorage.clear(); alert(localStorage.length); </script> </body> </html>
submitReset Code