简体   繁体   中英

Loading Javascript & HTML in webview

I'm having some trouble with getting Javascript to work in a Webview. Basically, what I'm doing is using a Webview Client and then loading an html file there:

super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mWebView = (WebView) findViewById(R.id.webview);
    mWebView.setWebViewClient(new HelloWebViewClient());
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setAllowFileAccess(true);
    mWebView.getSettings().setPluginsEnabled(true);
    mWebView.loadUrl("file:///android_asset/test/test.html");

The Webview loads perfectly and the html is displayed without problem.

In the html file, to load the javascript file I use

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

The Javascript file is also in the assets folder. The html/javascript works fine running it in a normal browser in my laptop, but I just can't get it to work in the Android. I've tried running it without setting a WebView client but no luck either.

Any help would be much appreciated.

您可以尝试使用对javascript文件的完整路径引用。

Just a suggestion! Change your folder sturcture to

 .../assets/www

Then try to put your js into a folder called "lib". .../assets/www/lib

then try the code like this

<script type="text/javascript" src="lib/test.js"></script>

change

mWebView.loadUrl("file:///android_asset/www/test.html");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM