Phonegap下localStorage使用实践

HTML5的Web Storage API提供了两种客户端存储数据的方法

localStorage和sessionStorage。 localStorage没有时间限制,程序升级也不会消失,可以满足持久化需求。

设置localStorage的方法

1
window.localStorage.setItem("likecolor","red");

获得localStorage的方法

1
window.localStorage.getItem("likecolor");

完整的Code如下:

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
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="msapplication-tap-highlight" content="no" />
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width" />
    <meta http-equiv="Content-Security-Policy" content="default-src * 'unsafe-inline'; style-src 'self' 'unsafe-inline'; media-src *" />
    <link rel="stylesheet" href="css/jquery.mobile-1.4.5.min.css">
    <script type="text/javascript" src="js/jquery.js"></script>
    <script  type="text/javascript" src="js/jquery.mobile-1.4.5.min.js"></script>
    <script type="text/javascript" charset="utf-8" src="cordova.js" ></script>
    <title>Hello World</title>
 
    <script type="text/javascript" charset="utf-8">
        document.addEventListener('deviceready', onDeviceReady, false);
        function onDeviceReady(){
             $(".localStorageBtn").bind("click",function(event,ui){
                  setLocalStorage();
             });
 
             $(".showLocalStorageBtn").bind("click", function(event, ui ) {
                  getLocalStorage();
             });
        }
 
         function setLocalStorage(){
            window.localStorage.setItem("likecolor","red");
         }
 
         function getLocalStorage(){
             var item = window.localStorage.getItem("likecolor");
             var info = document.getElementById("info");
             info.innerHTML = item;
         }
    </script>
</head>
 
<body>
<div id="page1" data-role="page" data-add-back-btn="true">
    <header data-role="header"><h1>Head</h1></header>
    <div data-role="content">
        <a data-role="button" class="localStorageBtn">set Local Storage</a>
        <a data-role="button" class="showLocalStorageBtn">get Local Storage</a>
        <p id="info"/>
    </div>
    <footer data-role="footer"><h1>Footer</h1></footer>
</div>
 
</body>
 
</html>

  


本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/p/6656234.html,如需转载请自行联系原作者

上一篇:input(type="file")+Handler(一般处理程序)上传文件简单Demo


下一篇:默认情况下,不使用of子句表示在select所有的数据表中加锁(转)