Calling JavaScript from Android
According to CW answer,
You need to wait until your page is loaded
private void helloJs(){
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
webView.evaluateJavascript("javascript:hello();", null);
} else {
webView.loadUrl("javascript:hello();");
}
}
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
helloJs();
}
});
Run javascript code in Webview
From kitkat onwards use evaluateJavascript method instead loadUrl to call the javascript functions like below
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
webView.evaluateJavascript("var FunctionOne = function () {"
+ " try{document.getElementsByClassName('test')[0].style.color='red';}catch(e){}"
+ "};", null);
} else {
webView.loadUrl("javascript:"
+ "var FunctionOne = function () {"
+ " try{document.getElementsByClassName('test')[0].style.color='red';}catch(e){}"
+ "};");
}
Enable Javascript for your webview by adding the following line
wb.getSettings().setJavaScriptEnabled(true);
It is possible to call a javascript function in android?
I believe you can use
WebView webView = (WebView) findViewById(R.id.webview);
webView.setWebChromeClient(new MyCustomChromeClient(this));
webView.getSettings().setJavaScriptEnabled(true);
webView.evaluateJavascript("globalJavascriptFunction();", null);
https://developer.android.com/reference/android/webkit/WebView#evaluateJavascript(java.lang.String,%20android.webkit.ValueCallback%3Cjava.lang.String%3E)
The second argument is a callback.
[edit]
To get a value back from the javascript, you can do something like this:
webView.evaluateJavascript("globalJavascriptFunction();", new ValueCallback<String>() {
@Override
public void onReceiveValue(String result) {
// result is the return value of globalJavascriptFunction()
// you'll have to do some testing inside here to figure out exactly how result gets sent back for your usecase
}
});
How to inject whole javascript function into webview and call it from android
Use the below mentioned method to inject a js file from asset folder
public void injectScriptFile(WebView view, String scriptFile) {
InputStream input;
try {
input = getAssets().open(scriptFile);
byte[] buffer = new byte[input.available()];
input.read(buffer);
input.close();
// String-ify the script byte-array using BASE64 encoding !!!
String encoded = Base64.encodeToString(buffer, Base64.NO_WRAP);
view.loadUrl("javascript:(function() {" +
"var parent = document.getElementsByTagName('head').item(0);" +
"var script = document.createElement('script');" +
"script.type = 'text/javascript';" +
// Tell the browser to BASE64-decode the string into your script !!!
"script.innerHTML = window.atob('" + encoded + "');" +
"parent.appendChild(script)" +
"})()");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Related Topics
Change an Image With Onclick()
How to Get Value of a Div Using JavaScript
How to Detect When a Page Exits Fullscreen
JavaScript - Shuffle HTML List Element Order
Best Way to Obfuscate an E-Mail Address on a Website
How to Force the Browser to Reload Cached CSS and JavaScript Files
How to Create a Sticky Navigation Bar That Becomes Fixed to the Top After Scrolling
How to Use Jquery to Select a Dropdown Option
Adding Additional Data to Select Options Using Jquery
Dynamically Loading CSS Stylesheet Doesn't Work on Ie
Jquery Event Won't Fire After Ajax Call
How to Display Image With JavaScript
How to Get the Background Color of an HTML Element
How to Open a Link in New Tab (And Not New Window)
Why Is Document.Write Considered a "Bad Practice"
Jquery Ajax Post Example With PHP
How to Avoid the Need For Ctrl-Click in a Multi-Select Box Using JavaScript