JS difference between local storage and session storage, and a cookie

window.sessionStorage property

localStorage and data storage properties allow sessionStorage key / value pairs in the browser.

To talk about the session storage (sessionStorage)

 

View the number of data stored:

sessionStorage .length

A data storage:

sessionStorage .['key'] = 'value';

Read data:

 sessionStorage.getItem('key'); 

    Data may = the sessionStorage var [ 'Key']; 

A data storage

sessionStorage.setItem('key','value');

Remove the specified data

sessionStorage.removeItem('value');

Delete all data

sessionStorage.clear()

 

In operation, using the methods and sessionStorage localStorage is the same;

Then take a look, we manipulate it will have any effect

 The following is a small word entry system

 

 

 

 At this local storage is empty, when I click save

 

 

 It will be credited to the local

 

 

 Characteristics of local storage is to close the current page data will not disappear, so I used to do word storage location

Next look Session store, I will test scores and the number of right and wrong into storage,

 

 

 

 

 

 Because the characteristics of the session is stored away from the current page will automatically delete these values, so I used to do record score

 

Since talking about here, we are concerned that the difference between the session storage, local storage and a cookie

The same point: are stored on the client

difference:

    1. Size

      cookie storage size of no more than 4KB

      sessionStorage and up to 5MB or more may localStorage;

    2. there is time data:

       LocalStorage:关闭浏览器数据依旧会存在,除非主动删除其中数据;

       sessionStorage:当前页关闭则数据删除;

       cookie:根据用户设置的cookie保存时间来定,在设定时间到来之前都会一直存在;

    3.数据与服务器之间的交互方式

        cookie的数据会自动传递到服务器,服务的也可以写到客户端

      LocalStroage和sessionStroage的数据仅保存在本地

Guess you like

Origin www.cnblogs.com/hzqzwl/p/12112043.html