HTML5 Local Storage Localstorage

Source: Internet
Author: User

1. Using localstorage requires first detection of the browser is supported

if (window.localstorage) {

Console.log ("browser support Localstorage")

}else{

Console.log ("Browser does not support Localstorage")

}

2.localStorage Write, read, delete (use local storage to add attributes to Window.localstorage)

if (window.localstorage) {

<1> Adding properties

Localstorage.name= "golden winged Bird";//Add the Name property

localstorage["Age"]=20;//add Age Property

Localstorage.setitem ("Sex", "male");//Add the Sex attribute----(This method is recommended for setting properties)

<2> Read Properties

var a1=localstorage.name;

var a2=localstorage["age";

var a3=localstorage.getitem ();//Recommended use of this method

<3> Delete Properties (remove all properties with clear ())

Localstorage.removeitem ();

}

The key () method provided by the 3.localStorage

if (window.localstorage) {

var storage=window.localstorage;

function Showstorage () {

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

Console.log (Storage.key (i) +storage.getitem (Storage.key (i)))

}

}

Showstorage ();

}

The 4.localStorage also provides a storage () event that allows you to listen for changes to key values

if (Window.addeventlistener) {

Window.addeventlistener ("Storage", handle_storage,false);

}else if (window.attachevent) {

Window.attacheevent ("Onstorage", Handle_storage)

}

function Handle_storage () {

if (!e) {e=window.event}

}

HTML5 Local Storage Localstorage

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.