HTML 5 Web storage and htmlweb Storage

Source: Internet
Author: User
Tags sessionstorage

HTML 5 Web storage and htmlweb Storage

HTML5 provides two new methods to store data on the client:

LocalStorage-data storage with no time limit
SessionStorage-data storage for a session

Html5 web storage browsers support judgment

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

If (window. localStorage ){
Alert ("localStorage 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

 

Http://hovertree.com/menu/html5/

Purpose: store the value to the key field.
Usage:. setItem (key, value)
Sample Code: sessionStorage. setItem ("key", "value"); localStorage. setItem ("site", "xiao ");


Get value through getItem

Purpose: Obtain the local storage value of a specified key.
Usage:. getItem (key)
Code example: var value = sessionStorage. getItem ("key"); var site = 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"]);

 

Recommended: http://www.cnblogs.com/roucheng/p/texiao.html

Related Article

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.