HTML5 's web Storage

Source: Internet
Author: User

What is HTML5 Web Storage?

Use the Html5,web page to save data locally using the user's browser.

In the past, we usually use cookies to store user data. However, using Web Storage is more secure and faster.

The data is no longer included in each server request, only when you need it. At the same time, we can also save a lot of data without compromising the performance of the site.

Data is saved in key/value form, and a Web page can only access its own data.

Browser support

Ie8+,firefox,chrome. Both opera and Safari 5 support this feature.

Note the IE7 and earlier version numbers do not support this feature.

Localstorage and Sessionstorage

Here are two new properties for saving data:

    • Localstorage-save data in a way that has no expiration time
    • Sessionstorage-Save data to session

Before using the Web storage, check to see if the browser supports Localstorage and sessionstorage:

 
   
  
  1. if (typeof(Storage)!=="undefined") {
  2. yes! Localstorage and Sessionstorage support!
  3. Some code .....
  4. }else{
  5. sorry! No Web Storage support.
  6. }
Localstorage Object

The Localstorage object holds the problem that the data has no expiration time.

The data is not deleted after the browser is closed. and has been effective.

 
   
  
  1. Localstorage . LastName = "Smith" ;
  2. Document . getElementById ("result"). InnerHTML = "Last name:"
  3. + Localstorage . LastName ;
Online Demo

Code Description:

    • A Localstorage key-value pair was created. Use key= "LastName", value= "Smith".
    • Get lastname corresponding value, and give Id=result element

Tip: Key-value pairs are stored as strings. Remember to convert them to other formats when necessary.

The following example calculates the number of times a user clicks a button.

In this code, the value will be converted to a number. This enables the use of addition:

  
 
  1. if (localstorage. Clickcount ){
  2. Localstorage . Clickcount = Number (localstorage. Clickcount ) +1;
  3. }else{
  4. Localstorage . Clickcount = 1 ;
  5. }
  6. Document . getElementById ("result"). InnerHTML = "You have clicked the button" + localstorage. Clickcount + "time (s)." ;
Online Demo Sessionstorage objects

Sessionstorage objects are similar to Localstorage objects except that the saved data is only valid in the current session. The data will be invalidated when the user closes the browser form.

The following code calculates the number of user clicks in the current session:

  
 
  1. if (sessionstorage. Clickcount ){
  2. Sessionstorage . Clickcount = Number (sessionstorage. Clickcount ) +1;
  3. }else{
  4. Sessionstorage . Clickcount = 1 ;
  5. }
  6. Document . getElementById ("result"). InnerHTML = "You have clicked the button" + sessionstorage. Clickcount + "time (s) in this session." ;

HTML5 's web Storage

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.