HTML5 provides four new methods to store data on the client, namely localStorage, sessionStorage, globalStorage, and Web SQL Database. This article first introduces the first three, which are relatively simple, easy to understand and operate. The next section focuses on Web SQL Database:
1. localStorage:
LocalStorage has no time limit for data storage. Data is still available on the second day, the second week, or after the next year. That is to say, localStorage will never expire unless the data is actively deleted. Data can be accessed and used across multiple Windows regardless of the current session. It is a bit like the global variable Application in the AspNet Application.
Ii. sessionStorage:
As its name implies, it is like a Session in AspNet. For data storage of a session, the information stored on any page of the same website in the window can access the data stored on it. The values of each window are independent, and its data is lost due to window close. sessionStorage between different windows cannot be shared.
LocalStorage/sessionStorage all have the same Api, such
LocalStorage. length get the number of storage
LocalStorage. key (n) gets the key of the nth key-Value Pair in storage
LocalStorage. key = value
Add localStorage. setItem (key, value)
Obtain localStorage. getItem (key)
LocalStorage. removeItem (key) Removal
LocalStorage. clear () clear
From the above Api, we can see that they are actually key/value pairs, dictionary, and JSON. Since it can be viewed as json, their operations can be as follows:
For example, localStorage. name = "Xu mingxiang"; // Add
LocalStorage ["name"] = "Xu mingxiang"; // Add
Alert (localStorage. name); // obtain www.2cto.com
Alert (localStorage ["name"]); // get
Iii. globalStorage:
After the browser is closed, the information stored using globalStorage can still be retained. Like sessionStorage, the information stored on any page in the domain can be shared by all pages. Currently, only FF is supported, and only globalStorage in the current domain is supported.
Basic usage:
GlobalStorage ['developer .mozilla.org '] -- all subdomains under pai.mozilla.org can read and write through this storage object.
GlobalStorage ['mozilla. org '] -- all webpages under the mozilla.org domain name can read and write through this storage object.
GlobalStorage ['org '] -- all webpages under the. org domain name can read and write through this storage object.
GlobalStorage [''] -- any webpage under any domain name can read and write through this storage object.
Method property:
SetItem (key, value) -- sets or resets the key value.
GetItem (key) -- get the key value.
RemoveItem (key) -- delete the key value.
Set the key value: window. globalStorage ["planabc.net"]. key = value;
Obtain the key value: value = window. globalStorage ["planabc.net"]. key;
4. Web SQL DataBase
Xu mingxiang