SessionStorage and localStorage in HTML5, html5localstorage

Source: Internet
Author: User
Tags delete key sessionstorage

SessionStorage and localStorage in HTML5, html5localstorage

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

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

WhileLocalStorageFor persistent local storage, data will never expire unless data is actively deleted.

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 StorageIt has setItem, getItem, removeItem, clear, and other methods. 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 actuallyJavascript Local Storage. 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)
Sample Code:

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

Purpose: Obtain the local storage value of a specified key.
Usage:. getItem (key)
Sample Code:

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

Purpose: Delete the local storage value of the specified key.
Usage:. removeItem (key)
Sample Code:

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

Purpose: Clear all keys/values.
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"]);
The key and length attributes of localStorage and sessionStorage are traversed.

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); }
Storage event

Storage also providesStorage eventWhen the key value is changed or clear, the storage event can be triggered. The following Code adds a listener for the storage event change:

if(window.addEventListener){ window.addEventListener("storage",handle_storage,false); }else if(window.attachEvent){ window.attachEvent("onstorage",handle_storage); } function handle_storage(e){ if(!e){e=window.event;} }

The specific attributes of the storage event object are as follows:

Property Type Description
Key String The named key that was added, removed, or moddified
OldValue Any The previous value (now overwritten), or null if a new item was added
NewValue Any The new value, or null if an item was added
Url/uri String The page that called the method that triggered this change

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.