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