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;
        }
2
  • Have you tried pushing it through the javascript: protocol? Commented Dec 16, 2011 at 11:00
  • atleast show wat have you done so far Commented Dec 16, 2011 at 11:01

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

But I want to add javascript file. how can I add it?
I tried this as per your suggestion, but not working at all, getting blank screen :(
Does this add the scripts to the webview?
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..

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.