I am trying to inject my external JS file (contained in assets dir) into WebView and call it afterwards.
This is the code I use for injecting it:
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
webView.loadUrl("javascript: (function() { "
+ "var script=document.createElement('script');"
+ "script.type='text/javascript';script.src='file://android_asset/js_demo.js';"
+ "document.getElementsByTagName('head').item(0).appendChild(script);"
+ "})()");
webView.loadUrl("javascript: jsDemo()");
}
});
When I print the entire content of my WebView, I can see that script tag with src='file://android_asset/js_demo.js' is indeed inserted, but calling function jsDemo does nothing.
NOTE: Function jsDemo is contained in js_demo.js and does nothing clever, just changes some span‘s color. It work ok since I tested it in browser.
I am convinced I made mistake giving the path to the js file, but I am not sure how to change it in order to make it work. Any help will be appreciated.
Why not just read in the file and execute it directly via
loadUrl("javascript:...)?