14

I am developing an application where in 1 part I want to add javascript into WebView.. But am not getting how to do it in an appropriate way.. Can anyone pls guide me into this?????

I am doing it like:

      wb=(WebView)findViewById(R.id.webView1);
        wb.getSettings().setJavaScriptEnabled(true);
        wb.getSettings().setPluginState(WebSettings.PluginState.ON);
        wb.getSettings().setPluginsEnabled(true);

        wb.loadUrl("javascript:<script " ></script> ");
        wb.setWebViewClient(new HelloWebViewClient());


 public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
Kanika
  • 10,648
  • 18
  • 61
  • 81

2 Answers2

12

It was very simple..also without using any javascriptInterface..In my code, Instead writing:

 wb.loadUrl("javascript:<script>   </script>");

use,

  wb.loadDataWithBaseURL(null,"<script>   </script>","text/html","utf-8",null);

and its working now :)

Kanika
  • 10,648
  • 18
  • 61
  • 81
0

Look at these nice tutorials about how to implements javascript in webview in android..

I think its provides you all the information what you needed..

Android WebView

Android WebView, Javascript and CSS

EDIT: Further if your implemented code having any exception or not working then please post that code and exception then here we can help you..

user370305
  • 108,599
  • 23
  • 164
  • 151
  • My code runs instantly when I add it to webview. How can I just add a function which can be latter called from html??....for example I want to add a function which will be called when user will fill recaptcha with same name I used in data-callback of recaptcha – Zohab Ali Jul 28 '18 at 11:26