最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Android webview load local javascript - Stack Overflow

programmeradmin4浏览0评论

I am trying to load a javascript file stored on the device via html file which is loaded via a webview but never seems to load. I have tried using direct url's like you normally would in html and have also tried:

<script type="text/javascript" src="file:///android_asset/www/js/jsfile.js"/>

JavaScript is enabled on the webview settings too and works fine if I have it on a server.

Thanks if anyone can help.

I am trying to load a javascript file stored on the device via html file which is loaded via a webview but never seems to load. I have tried using direct url's like you normally would in html and have also tried:

<script type="text/javascript" src="file:///android_asset/www/js/jsfile.js"/>

JavaScript is enabled on the webview settings too and works fine if I have it on a server.

Thanks if anyone can help.

Share Improve this question asked Jun 30, 2011 at 10:11 RichRich 5432 gold badges6 silver badges19 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 15

Hi actually I thing you should call directly the js file because you are calling it from the browser which considers the asset folder being its root folder. You should use the "file:///" prefix when calling from java code. Try something like this:

<script type="text/javascript" src="www/js/jsfile.js"/>

You can use loadDataWithBaseURL.

Put all your javascript under an assets folder and give js file path relative to the assets directory in your script tag (in the html). Don't put a slash in the beginning of src.

Read the html into a string (htmlStr) and then load it in the webview as mentioned below.

webView.loadDataWithBaseURL("file:///android_asset/", htmlStr, "text/html", "UTF-8", null);

It has worked for me.

发布评论

评论列表(0)

  1. 暂无评论