HTML5 Localstorage Local Storage principle detailed

Source: Internet
Author: User

First, it is natural to detect whether the browser supports local storage. In HTML5, the local storage is a window property, including Localstorage and Sessionstorage, from the name should be able to clearly identify the difference between the former is always local, the latter is only accompanied by the session, the window once closed. The use of the two is exactly the same, for example, Localstorage.

if (window.localstorage) {

Alert (' This brower supports Localstorage ');

}else{

Alert (' This brower not supports Localstorage)

}

The way to store data is to add a property directly to Window.localstorage, for example: Window.localstorage.a or window.localstorage["a"]. Its read, write, delete operation method is very simple, is in the way of key-value pairs exist, as follows:

Localstorage.setitem ("A", "Isaac");//Set the value of A to "Isaac"

var a = Localstorage.getitem ("a");//Get the value of a

Localstorage.removeitem ("a");//Clear the value of a

The most recommended natural use is GetItem () and SetItem (), which clears the key value pair using RemoveItem (). If you want to clear all key-value pairs at once, you can use clear (). In addition, HTML5 also provides a key () method that can be used when you do not know what key values are available, as follows:

var storage = Window.localstorage;

function Showstorage () {

for (var i = 0;i<storage.length;i++) {

Key (i) obtains the corresponding key, then uses the GetItem to obtain the corresponding key value

document.write (Storage.key (i) + ":" +storage.getitem (Storage.key (i) + "<br>")

}

}

It is important to note that HTML5 local storage can only store strings, and any format stored will be automatically converted to strings, so when reading, you need to do a type of conversion. This is why parseint must be used in the previous code.

In addition, on the Iphone/ipad sometimes set SetItem () will appear strange quota_exceeded_err error, then generally before SetItem, first RemoveItem () ok.

HTML5 Localstorage Local Storage principle detailed

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.