Localstorage and sessionstorage usage records (recommended), sessionlocalstorage

Source: Internet
Author: User
Tags sessionstorage

Localstorage and sessionstorage usage records (recommended), sessionlocalstorage

By reading the Usage Analysis of locastorage and sessionstorage on web storage, I tried it myself and made a note here.

If a project uses storage for many times and stores a lot of data, it should be encapsulated into a function:

(This function is written by an unknown great god. If there is any violation of originality, please contact me ......)

Function setStorage (key, value) {if (! Window. localStorage) {alert ("the browser does not support localstorage"); return false;} else {var storage = window. localStorage; // write the field storage. setItem (key, value) ;}} function getStorage (key) {if (! Window. localStorage) {alert ("the browser does not support localstorage");} else {var storage = window. localStorage; var key = storage. getItem (key); // console. log (key); return key ;}}

SetStorage stores data, and the key is the specified data name. It can be set as needed, but it must be a string type. Otherwise, the browser automatically uses the value as the key name.

The first value is that the key is not specified as a string, that is, no double quotation marks are added.

Double quotation marks must also be added for string types of value values.

How can I view storage in a browser?

View the location of a newer version of chrome browser

If the same data storage function is called multiple times in a project, the data changes in real time. If you need to clear all stored data:

Localstorage. clear (); or sessionStorage. clear ();

The principle used in the project is to use the data to call the function for storing the data.

The above section describes the usage records of localstorage and sessionstorage (recommended). I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.