SessionStorage and localStorage usage in JavaScript

Source: Internet
Author: User
Tags delete key sessionstorage

Web Storage in html5 includes two Storage methods: sessionStorage and localStorage.

SessionStorage is used to locally store data in a session. The data can be accessed only on pages in the same session. When the session ends, the data is also destroyed. SessionStorage is not a persistent local storage, but a session-level storage.

LocalStorage is used for persistent local storage. Unless data is actively deleted, the data will never expire.

Differences between web storage and cookie

The concept of Web Storage is similar to that of cookie. The difference is that it is designed for larger Storage capacity. The Cookie size is limited, and the Cookie will be sent every time you request a new page. This will result in a waste of bandwidth. In addition, the cookie also needs to specify the scope, cross-Origin calls are not allowed.

In addition, Web Storage has methods such as setItem, getItem, removeItem, and clear. Unlike cookies, front-end developers need to encapsulate setCookie and getCookie by themselves.

However, cookies are indispensable: cookies interact with servers and exist as part of HTTP specifications, web Storage is only generated to "store" data locally (from @ otakustay)

Browser support for html5 web storage

In addition to IE7 and below, browser support is fully supported by other standard browsers (ie and FF must be run on the web server). It is worth mentioning that IE is always a good thing, for example, UserData in IE7 and IE6 is actually a local javascript storage solution. Through simple code encapsulation, all browsers support web storage.

You can use the following code to determine whether the browser supports localStorage:

If (window. localStorage) {alert ("locallocallocallocallocalstorage")} else {alert (" locallocallocallocallocalstorage")} // or if (typeof window. localStorage = 'undefined') {alert ("localStorage is not supported for browsing")} localStorage and sessionStorage operations

Both localStorage and sessionStorage have the same operation methods, such as setItem, getItem, and removeItem.

LocalStorage and sessionStorage methods

SetItem storage value

Purpose: store the value to the key field usage:. setItem (key, value) code example:

SessionStorage. setItem ("key", "value"); localStorage. setItem ("site", "js8.in ");
Get value through getItem

Purpose: obtain the value of a specified key. Usage:. getItem (key) code example:

Var value = sessionStorage. getItem ("key"); var site = localStorage. getItem ("site ");
RemoveItem delete key

Purpose: delete the value of a specified key. Usage:. removeItem (key) code example:

SessionStorage. removeItem ("key"); localStorage. removeItem ("site ");
Clear all key/value

Purpose: clear all key/value usage:. clear () sample code:

SessionStorage. clear (); localStorage. clear ();
Other operations: click operations and []

Web Storage can not only use its own setItem, getItem, and other convenient access, but also use points (.) like common objects (.) operator, and the [] method for data storage, as shown in the following code:

Var storage = window. localStorage; storage. key1 = "hello"; storage ["key2"] = "world"; console. log (storage. key1); console. log (storage ["key2"]); implements traversal of the key and length attributes of localStorage and sessionStorage

The keys () and length provided by sessionStorage and localStorage can be used to conveniently traverse stored data. For example, the following code:

Var storage = window. localStorage; for (var I = 0, len = storage. length; I <len; I ++) {var key = storage. key (I); var value = storage. getItem (key); console. log (key + "=" + value );}

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.