之前在做html之間進行傳輸數值的時候,用的是尾碼問號的方法,即形如:a.html?id=1 這種形式。
然後辛辛苦苦把全部網頁做完並在pc端瀏覽器調試成功之後,封裝到android中,把所有的html檔案放進assets檔案夾內,並通過webview來訪問首頁index.html。用android studio配置好並下載到機器(安卓版本很低)裡面,運行也很成功。。。
但是,有一個大問題就是,尾碼問號來傳輸數值的方法並不能相容。在android裝置裡無法訪問帶有。的網站地址。一下子懵逼了…..
網上百度了很多資料,發現android3.0以後就不在支援這種方式了。
參考:http://blog.csdn.net/wenfengzhuo/article/details/7924807
要想實現此功能,看來是必須要經過app了。。。可是這就意味著要重新寫代碼而且要調試android。
參考:http://www.oschina.net/question/931234_2142629
http://zhidao.baidu.com/link?url=8paS-GxU7Ma2kY3zJr91O1M0UoRVoagFoHofuOqEGDHqHlCmfBtTDdfjKI-HpeNs4OJxhJxK80j3-3TNZft9bq
http://blog.csdn.net/li_li_lin/article/details/39484721
http://blog.csdn.net/qduningning/article/details/43411765
http://blogs.360.cn/360mobile/2014/09/22/webview%E8%B7%A8%E6%BA%90%E6%94%BB%E5%87%BB%E5%88%86%E6%9E%90/
http://blog.csdn.net/kevinscsdn/article/details/52241334
於是,想了一個方法….既簡單又方便:localstorage。
只需要在MainActivity.java裡聲明webview可以使用localstorage即可:
webview.getSettings().setJavaScriptEnabled(true);webview.getSettings().setDomStorageEnabled(true);
然後只需要寫js就可以了,而不需要再通過android了。
附js代碼:
a.html
<body><a href="b.html" onclick="content()">提交跳轉傳值</a> </body><script type="text/javascript"> window.onload = function() { } function content() { var zhi1 = document.getElementById("id1").value; var zhi2 = document.getElementById("id2").value; localStorage.setItem("1", id1); localStorage.setItem("2", id2); } </script>
b.html
<script type="text/javascript"> window.onload=function(){ var a=localStorage.getItem("1"); var b=localStorage.getItem("2"); document.getElementById("jieshouzhi1").value=a; document.getElementById("jieshouzhi2").value=b; } </script>