H5 Local Storage Storage

Source: Internet
Author: User
Tags sessionstorage

Storage local storage, it seems a bit reminiscent of cookies, and indeed usage is similar to cookies. But storage has many advantages over cookies:

Large storage capacity; The client is not requested;

Storage is divided into Sessionstorage and localstorage.

1.sessionStorage temporary storage, the local storage disappears when the page is closed. and sessionstorage data is not shared.

2.localStorage permanently deleted, you can manually delete the data, the data is shared.

The following APIs are available under storage:

Window.sessionstorage and Window.localstorage have the following 4 methods:

SetItem (): Set data, Key\value type, type all strings can be manipulated in the form of Get properties

GetItem (): Gets the data through key to get to the corresponding value RemoveItem ():

Delete the data, delete the corresponding value by key

Clear (): deletes all stored values. Use the following:

Window.localStorage.getItem (' name ')

Storage events:

The storage event is triggered when the data is modified or deleted. However, in the Window object to change the data will not be triggered, that is, there are two and more than two windows, when there is data changes, in addition to their own window unexpectedly other window objects will trigger the storage event.

The event object has the following properties under it:

Key: The key value is modified or deleted, if clear () is called, key is null

NewValue: The newly set value, if called removestorage (), Key is null

OldValue: The value before the change is called

Storagearea: The current storage object

URL: The URL of the document that triggered the change in the script

As follows:

Window.addeventlistener (' storage ',function(EV) {  /// Current page events do not touch                 Console.log (Ev.key);        Console.log (ev.newvalue);        Console.log (ev.oldvalue);        Console.log (Ev.storagearea);        Console.log (Ev.url);            },false);

H5 Local Storage Storage

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.