Web Local Storage (localstorage, sessionstorage)

Source: Internet
Author: User
Tags sessionstorage

For browsers, using Web Storage to store key values compared to storing cookies is more intuitive and more capacity, it contains two kinds: Localstorage and Sessionstorage

    1. Sessionstorage (Temporary storage): maintains a storage area for each data source that exists during browser opening, including page reload

    2. Localstorage (long-term storage): Same as Sessionstorage, but when the browser is closed, the data remains

1. Save data to Local

Sessionstorage.setitem (' key ', ' your Data ');

Localstorage.setitem (' key ', ' your Data ');

2. Getting data from local storage

var data1 = Json.parse (sessionstorage.getitem (' key '));

var data2 = Json.parse (localstorage.getitem (' key '));

3. Delete a saved data from the local store

Sessionstorage.removeitem (' key ');

Localstorage.removeitem (' key ');

4. Delete all saved data

Sessionstorage.clear ();

Localstorage.clear ();

5. Monitor changes to local storage

Description: Storage changes (add, update, delete) when the trigger, the same page changes will not trigger, will only listen to the same domain name other page changes Storage

Window.addeventlistener (' storage ', function (e) {

Console.log (' key ', e.key);

Console.log (' OldValue ', e.oldvalue);

Console.log (' newvalue ', e.newvalue);

Console.log (' url ', e.url);

})

6. Browser Viewing methods

1. Go to Developer tools

2. Select Application

3. Check the Local Storage and Session Storage on the left Storage

Web Local Storage (localstorage, sessionstorage)

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.