HTML5 development and learning (2): localStorage, sessionStorage, and globalStorage

Source: Internet
Author: User
Tags sessionstorage

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

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.