2014-01-16 71 views
0

我必须在我的phonegap(3.0)应用程序中使用localstorage,以便我可以在每个页面中调用用户希望的具体数字。为了做到这一点,我使用下面的表单和脚本。这个脚本在浏览器中工作,但不能用phonegap构建。我应该错过一些东西,但找不到什么!Phonegap和LocalStorage问题,但在浏览器中工作

<!DOCTYPE html> 
<html lang="fr"> 
<head> 
    <title>local storage</title> 
    <meta charset="utf-8"> 
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0b3/jquery.mobile-1.0b3.min.css"/> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.3.min.js"></script> 
    <script type="text/javascript" src="http://code.jquery.com/mobile/1.0b3/jquery.mobile-1.0b3.min.js"></script> 
     <script type="text/javascript" charset="utf-8" src="cordova.js"></script> 
    <style> 
     #message { 
      display: none; 
      border-radius: 10px; 
      padding: 10px; 
      background-color: #ff0000; 
      color: #fff; 
     } 

     #storageDemoPage h2 { 
      white-space: normal; 
     } 
    </style> 
    <script type="text/javascript"> 
     var localStorageKey = "allo"; 
     $('#storageDemoPage').live('pagecreate', function() { 
       showStoreValue(); 
      $('#addToStorage').click(function(e) { 
       localStorage.setItem(localStorageKey, $('#entry').val()); 
       showStoreValue(); 
       e.preventDefault(); 
      }); 
     }); 

     function showStoreValue() { 
      var item = localStorage.getItem(localStorageKey); 
      if (item == null) { 
       item = 'Pas de valeur'; 
      } 
      else if (item.length === 0) { 
       item = 'Aucune valeur d\'enregistrée'; 
      } 
      $('.storeItem').text(item); 
     } 
    </script> 
</head> 
<body> 
<section data-role="page" id="storageDemoPage"> 
    <section data-role="header"> 
     <h2>My app</h2> 
    </section> 
    <section data-role="content"> 
     <p id="message"/> 
     <ul data-role="listview" data-inset="true"> 
      <li data-role="list-divider">Enter your number</li> 
      <li> 
       <input type="text" id="entry" name="entry" placeholder="Write digits"/> 
       <input type="button" id="addToStorage" value="Store value"/> 
      </li> 
      <li data-role="list-divider">Value record</li> 
      <li class="storeItem"/> 
     </ul> 
      <div data-role="content"> 
      <p id="audio_position"></p> 
      <ul id="allRepos" data-role="listview"></ul> 
      </div> 
    </section>  
</body> 
</html> 

编辑:在开始时

+0

在PhoneGap的尝试,你访问的localStorage之前听deviceready而不是pagecreate。 – QuickFix

+0

这不起作用的是你的想法? var localStorageKey =“allo”; document.addEventListener(“showStoreValue”,onDeviceReady,false); 。 $( '#storageDemoPage')生活( 'onDeviceReady',函数(){ showStoreValue(); $( '#addToStorage')点击(函数(E){ localStorage.setItem(localStorageKey,$( ')。val()); showStoreValue(); e.preventDefault(); }); }); –

回答

1

感谢@QuickFix和@克里斯这里是解决方案:

<script type="text/javascript"> 
    // Wait for device API libraries to load 
    // 
    document.addEventListener("deviceready", onDeviceReady, false); 

    // device APIs are available 
    // 
    var localStorageKey = "allo"; 

    function onDeviceReady() { 
     showStoreValue(); 
      $('#addToStorage').click(function(e) { 
       window.localStorage.setItem(localStorageKey, $('#entry').val()); 
       showStoreValue(); 
       e.preventDefault(); 
      }); 

    } 

     function showStoreValue() { 
      var item = localStorage.getItem(localStorageKey); 
      if (item == null) { 
       item = 'Pas de valeur'; 
      } 
      else if (item.length === 0) { 
       item = 'Aucune valeur d\'enregistrée'; 
      } 
      $('.storeItem').text(item); 
     }  
    </script> 
0

这里补充科尔多瓦脚本,你会在你的脑袋标签已经错过

一)科尔多瓦脚本文件包含可能的事情。

b)在使用本地存储之前使用并监听设备。

实施此应该可以解决您的问题。

+0

嗨克里斯,当然,我在我的开发应用程序中有cordova脚本,你说得对,我应该包括在这里(我会更新问题)。我会尝试b解决方案,感谢您的帮助 –

相关问题