Android -WebView how to load javascript from assets folder
After the webview completes loading a URL im trying to inject my
javascript file which i have saved in assets/www/javascriptfile.js
this is the javascript im trying to inject which i thought would load all
my javascript functions in javascriptfile.js (making my code more neat):
var script = document.createElement('script');
script.setAttribute('src', 'file:///android_asset/www/javascriptfile.js');
script.setAttribute('type', 'text/javascript');
document.body.appendChild(script);
Here is what the code looks like in my subclass of WebViewClient:
public void onPageFinished(WebView view, String url) {
webView.loadUrl("javascript:(function() { "
+ "var script=document.createElement('script'); "
+ " script.setAttribute('type','text/javascript'); "
+ " script.setAttribute('src',
'file:///android_asset/www/javascriptfile.js'); "
+ " script.onload = function(){ "
+ " changeBGC('#000099'); "
+ " }; "
+ "document.body.appendChild(script); "
+ ")();");
}
i use script.onload so that i can test one of the functions im defning.
Nothing ever runs. the initial HTML url runs and i tried both local html
and external, makes no difference.
this is what the javascriptfile.js looks like:
function changeBGC(color){
document.bgColor = color;
}
CAN SOMEONE HELP ME TO INJECT JAVASCRIPTFILE.JS INTO THE WEBVIEW AFTER LOAD ?
No comments:
Post a Comment