在android系統內webview中的html檔案之間如何進行數值的傳輸

來源:互聯網
上載者:User

之前在做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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.