Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
62
rated 0 times [  67] [ 5]  / answers: 1 / hits: 28287  / 9 Years ago, sun, august 23, 2015, 12:00:00

I have a webview i am using in android and I'm trying to trigger javascript on a button click. I'm trying to use the code below to change the color of the class to red. But I cant seem to get it working



final WebView wb=(WebView)findViewById(R.id.webView2);
wb.loadUrl(javascript:
+ var FunctionOne = function () {
+ try{document.getElementsByClassName('test')[0].style.color='red';}catch(e){}
+ };);

More From » android

 Answers
26

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);

[#65323] Thursday, August 20, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jimmieo

Total Points: 515
Total Questions: 102
Total Answers: 110

Location: Kazakhstan
Member since Mon, Sep 26, 2022
2 Years ago
;