Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android webview load local javascript

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.

like image 778
Rich Avatar asked Jun 30 '11 10:06

Rich


People also ask

How do I enable JavaScript on Android WebView?

JavaScript is disabled in a WebView by default. You can enable it through the WebSettings attached to your WebView . You can retrieve WebSettings with getSettings() , then enable JavaScript with setJavaScriptEnabled() . WebView myWebView = (WebView) findViewById(R.

How do I open local PDF files on Android?

To find out if you have an app that can open PDFs, just try to do so. Navigate to the file manager on your Android device and find a PDF file. Any apps that can open PDFs will appear as choices. Simply select one of the apps and the PDF will open.


1 Answers

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"/>
like image 173
Paul Avatar answered Oct 14 '22 20:10

Paul