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