WebView載入本地頁面js互動問題

2021-10-03 03:14:19 字數 1403 閱讀 3924

1.開啟本包內assets目錄下的index.html檔案

webview.loadurl("file:///android_asset/index.html");
2.開啟本包內/data/data/pkg/files目錄目錄下的index.html檔案

webview.loadurl("file:"+getfilesdir()+file.separator+"index.html");
3.開啟本包內/data/data/pkg/cache目錄目錄下的index.html檔案

webview.loadurl("file:"+getcachedir()+file.separator+"index.html");
4.開啟sd卡根目錄下的index.html檔案

webview.loadurl("file:"+environment.getexternalstoragedirectory()+file.separator+"index.html");
5.開啟本包內sdcard/android/data/pkg/files/ 目錄下的index.html檔案

webview.loadurl("file:"+context.getexternalfilesdir()+file.separator+"index.html");

6.開啟本包內sdcard/android/data/pkg/cache/ 目錄下的index.html檔案

webview.loadurl("file:"+context.getexternalcachedir()+file.separator+"index.html");
以上是基本得載入方法,網上很多資料,這裡說下如果載入本地網頁中有使用html本地儲存的情況,

介紹:html5 是下一代 html 標準,開始吸引越來越多人的目光。html5 的 dom storage 機制提供了一種方式讓程式設計師能夠把資訊儲存到本地的計算機上,在需要時獲取。這點和 cookie 相似,區別是 dom storage 提供了更大容量的儲存空間。 

dom storage 分為 sessionstorage 和 localstorage。 

localstorage 物件和 sessionstorage 物件使用方法基本相同,它們的區別在於作用的範圍不同。sessionstorage 用來儲存與頁面相關的資料,它在頁面關閉後無法使用。而 localstorage 則持久存在,在頁面關閉後也可以使用。

使用html5的 localstorage 存不進去也讀不出來

加上mwebview.getsettings().setdomstorageenabled(true);

WebView 載入本地的html

1 可以是用loaddata,這種方法需要先將html檔案讀取出來,以字串傳入loaddata,可以展示頁面,但是不會引用css js等檔案。2 使用loadurl,不過需要注意,這裡因為是使用本地資料,所以傳入的url需要做些處理,例如 a 如果html檔案存於assets 則加字首 file a...

webview載入優化

override public webresourceresponse shouldinterceptrequest webview view,string url else if url.contains 20.b7e931615e2f93316749.js else if url.contain...

js頁面載入函式

在未載入完文件,使用jquery選擇器選擇元素後,如果立即繫結事件進行呼叫,會引起js的報錯 can not read property of undefined 導致事件不能繫結成功.alert p 1 firstchild.nodevalue 這時就需要js的頁面載入函式 一般有三種 windo...