HTML5 Localstorage Local Storage

Source: Internet
Author: User
Tags sessionstorage

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

if (window.localstorage) {
Alert (' This browser supports Localstorage ');
}else{
Alert (' This browser does not support 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.a = 3;// set a to "3"
Localstorage["a"] = "SFSF";// set a to "SFSF", overriding the above value
Localstorage.setitem ("B", "Isaac");// set b to "Isaac"
var A1 = localstorage["A"];// gets the value of a
var a2 = localstorage.a;// gets the value of a
var B = Localstorage.getitem ("b");/ / get the value of B
Localstorage.removeitem ("C");/ / clears the value of C

Self-written examples:

<script type= "Text/javascript" src= "Jquery.js" ></script>
<script>

Localstorage.a = 3;//Set A to "3"
Localstorage["a"] = "SFSF";//Set A to "SFSF", overriding the above value
Localstorage.setitem ("B", "Isaac");//set B to "Isaac"

$ (function () {

$ (". Kucun1"). Click (function () {
var A1 = localstorage["A"];//gets the value of a
var a2 = localstorage.a;//Gets the value of a

alert (A1);
Sessionstorage.setitem ("A1", "Sssss");
})
})

function Txtname_change (v) {
if (window.sessionstorage) {
Alert (' This browser supports Localstorage ');
}else{
Alert (' This browser does not support Localstorage ');
}
var strName =v.value;//get text box contents
Sessionstorage.setitem ("StrName", 212); Call the SetItem (Key,value) method to save the contents to the session object with the key named strname
var a1 = Sessionstorage.getitem ("StrName");
$ ("#pStatus"). Text (Sessionstorage.getitem ("StrName")); The saved content is obtained by key name and is displayed in the page (Span tab) via the key number
alert (A1);
Window.location.href= "http://localhost:801/a.php";
}

Localstorage.a = 3;//Set A to "3"
Localstorage["a"] = "SFSF";//Set A to "SFSF", overriding the above value
Localstorage.setitem ("B", "Isaac");//set B to "Isaac"
var A1 = localstorage["A"];//gets the value of a
var a2 = localstorage.a;//Gets the value of a

</script>

<div class= "Kucun" id= "DSD" onclick= "Txtname_change ()" >
Dddddddddd
</div>

It should be noted that HTML5 local storage can only save strings, any format stored in the time will be automatically converted to a string, so when reading, you need to do the type of conversion

HTML5 Localstorage Local 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.