1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
<!DOCTYPE html>
<
html
>
<
body
>
<
script
>
// validate thether this browser support storage
if(window.localStorage){
console.log("support local storage");
}
if(window.sessionStorage){
console.log("support session storage");
}
addEntryForSessionStorage();
addEntryForLocalStorage();
printAllLocalStoreInfo();
printAllSessionStoreInfo();
/* ============================== session storage ============================== */
function addEntryForSessionStorage(){
var key = window.sessionStorage.length+1;
var value = parseInt(Math.random()*1000);
window.sessionStorage.setItem(key,value);
}
function printAllSessionStoreInfo(){
console.log("all session storage informaton is below.");
var sessionStorage = window.sessionStorage;
for(var i=0;i<
sessionStorage.length
;i++){
var key = sessionStorage.key(i);
var value = sessionStorage.getItem(key);
console.log(key+":"+value);
}
}
/* ============================== local storage ============================== */
function addEntryForLocalStorage(){
var key = window.localStorage.length+1;
var value = parseInt(Math.random()*1000);
window.localStorage.setItem(key,value);
}
function printAllLocalStoreInfo(){
console.log("all local storage informaton is below.");
var localStorage = window.localStorage;
for(var
i
=
0
;i<localStorage.length;i++){
var key = localStorage.key(i);
var value = localStorage.getItem(key);
console.log(key+":"+value);
}
}
</script>
</
body
>
</
html
>
|
本文转自 antlove 51CTO博客,原文链接:http://blog.51cto.com/antlove/1675258