HTML 5 Web storage-localStorage

Source: Internet
Author: User
Tags website performance

HTML 5 Web storage-localStorage

Today, when calling an angularJs code, we can see localStorge:

Because this object is not defined in the service, I thought it was an object injected by ag for me at first, but I did not find the definition of this object even though I went through all js Code. I checked and found that I was out again:

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

LocalStorage-sessionStorage-data storage for one session

Previously, these are all completed by cookies. However, cookies are not suitable for storing a large amount of data because they are transmitted by each request to the server, which makes the cookie speed slow and inefficient.

In HTML5, data is transmitted not by each server request, but only by the request. It makes it possible to store a large amount of data without affecting the website performance.

It turned out to be a new H5 object. Compared with the 4 K cookie, the 5 m capacity is very large.

The following provides some commonly used scripts:

/* Check whether the browser supports localStorage */if (window. localStorage) {alert ('this browser supports localstore');} else {alert ('this browser does not support localstore');}/* test adding localStorage */localStorage. a = 'lhc '; // method alert (localStorage. a); localStorage ["B"] = "pbccccc"; // method 2 alert (localStorage ["B"]);/* Get, update, delete localstorge */alert (localStorage. getItem ("a"); // obtain the value of localStorage. setItem ("a", "lhccccccccc"); // set the value of a (localStorage. a); localStorage. removeItem ("a"); // remove

In addition, there is no time limit on the data stored by the localStorage method. Data is still available on the second day, the second week, or after the next year. It is much easier to use than the cookie before long ago (at least it is unnecessary for the browser to disable this function ).

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.