HTML5 sessionstorage and localstorage

Source: Internet
Author: User
Tags delete key sessionstorage
ArticleDirectory
    • Localstorage and sessionstorage Methods
    • Storage event

Web storage in HTML5 includes two storage methods: sessionstorage and localstorage.

SessionstorageIt is used to locally store data in a session. The data can be accessed only on pages in the same session, and the data will be destroyed after the session ends. Sessionstorage is not a persistent local storage, but a session-level storage.

WhileLocalstorageFor persistent local storage, data will never expire unless data is actively deleted.

Differences between Web storage and cookie

The concept of Web storage is similar to that of cookie. The difference is that it is designed for larger storage capacity. The cookie size is limited, and the cookie will be sent every time you request a new page. This will result in a waste of bandwidth. In addition, the cookie also needs to specify the scope, cross-origin calls are not allowed.

In addition,Web StorageIt has setitem, getitem, removeitem, clear, and other methods. Unlike cookies, front-end developers need to encapsulate setcookie and getcookie by themselves.

However, cookies are indispensable: cookies interact with servers and exist as part of HTTP specifications, web storage is only generated to "Store" data locally (from @ otakustay)

Browser support for HTML5 web Storage

In addition to IE7 and below, browser support is fully supported by other standard browsers (ie and FF must be run on the Web server). It is worth mentioning that IE is always a good thing, for example, userdata in IE7 and IE6 is actuallyJavascript Local Storage. Through simpleCodeEncapsulation can be unified to all browsers that support web storage.

You can use the following code to determine whether the browser supports localstorage:

 If  ( Window. Localstorage  )  {       Alert  (  "Localstorage is supported for browsing"  )   }  Else  {     Alert  (  "Localstorage is not supported for browsing"  )   }   // Or   If  (  Typeof Window. Localstorage   =   'Undefined'  )  {   Alert  ( "Localstorage is not supported for browsing"  )   } 
Localstorage and sessionstorage operations

Both localstorage and sessionstorage have the same operation methods, such as setitem, getitem, and removeitem.

Localstorage and sessionstorage Methods setitem storage value

Purpose: store the value to the key field.
Usage:. setitem (Key, value)
Sample Code:

Sessionstorage.Setitem("Key", "Value");Localstorage.Setitem("Site", "Js8.in");
Get value through getitem

Purpose: Obtain the local storage value of a specified key.
Usage:. getitem (key)
Sample Code:

VaRValue=Sessionstorage.Getitem("Key"); VaRSite=Localstorage.Getitem("Site");
Removeitem delete key

Purpose: Delete the local storage value of the specified key.
Usage:. removeitem (key)
Sample Code:

Sessionstorage.Removeitem("Key");Localstorage.Removeitem("Site");
Clear all key/value

Purpose: Clear all keys/values.
Usage:. Clear ()
Sample Code:

 
Sessionstorage.Clear();Localstorage.Clear();
Other operations: Click operations and []

Web storage can not only use its own setitem, getitem, and other convenient access, but also use points (.) like common objects (.) operator, and the [] method for data storage, as shown in the following code:

 VaR Storage = Window. Localstorage  ; Storage. Key1   =   "Hello"  ; Storage [  "Key2"  ]   =   "World" ; Console. Log  ( Storage. Key1  )  ; Console. Log  ( Storage [  "Key2"  ]  )  ; 
The key and length attributes of localstorage and sessionstorage are traversed.

The keys () and length provided by sessionstorage and localstorage can be used to conveniently traverse stored data. For example, the following code:

 VaR Storage = Window. Localstorage  ;   For   (  VaR I =  0  , Len = Storage. Length  ; I < Len ; I++  )  {       VaR Key = Storage. Key  ( I )  ;       VaR Value = Storage. Getitem  ( Key )  ; Console.Log  ( Key +   "="   + Value )  ;   } 
Storage event

Storage also providesStorage eventWhen the key value is changed or clear, the storage event can be triggered. The following Code adds a listener for the storage event change:

 If  ( Window. Addeventlistener  )  { Window.Addeventlistener  (  "Storage"  , Handle_storage ,  False  )  ;   }  Else   If  ( Window. Attachevent  )  { Window.Attachevent  (  "Onstorage"  , Handle_storage )  ;   }   Function Handle_storage ( E )  {   If  (  ! E )  { E = Window. Event  ;  }   } 

The specific attributes of the storage event object are as follows:

Property Type Description
Key String The named key that was added, removed, or moddified
Oldvalue Any The previous value (now overwritten), or null if a new item was added
Newvalue Any The new value, or null if an item was added
URL/uri String The page that called the method that triggered this change
Web storage demo

HTML5 demos: storage web storage example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.