Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call javascript function in Android WebView

I have an activity with WebView and Button on it. Android sdk 17. Website isn't mine, so I can't change it anyway. I need to do js code by android button click.

I'm trying to do this

public class RostelecomLoginActivity extends Activity {

WebView webView;
String url;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_rostelecom_login);

    Intent webIntent = getIntent();
    final String url = webIntent.getStringExtra("url");

    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setSaveFormData(true);
    webView.getSettings().setSavePassword(true);

    Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            webView.addJavascriptInterface(new Object(){
                @JavascriptInterface
                public void test(){
                    Log.d("JS", "test");
                }
            },"Android");
            webView.loadUrl("javascript:(function(){document.getElementById('mA').click();})()");
        }
    });

    webView.loadUrl(url);

}}

But it doesn't do anything. How can I call my js right?

like image 339
Valeriy Avatar asked Jun 10 '13 12:06

Valeriy


People also ask

How do I call a JavaScript function from a WebView?

As you can see, a function that’ll be called from JavaScript has the special annotation @JavascriptInterface, which indicates to WebView that this function can be called from JS. The next thing to do is to add this interface to WebView. Here we name it Android, but you can name it as you want.

How do I add JavaScript to Android WebView?

Binding JavaScript to Android WebView allows you to bind JavaScript code to Android code through an interface. To do this, we must use the addJavaScriptInterface () method, which is passed the class that provides the interface for JS, and the name that will be used to display the instance in JS (for example, “ AndroidFunction “).

How to enable JavaScript on a destroyed WebView?

If the WebView has been destroyed, any call to the WebSettings method will raise an IllegalStateException. To use JavaScript, you need to enable it by calling the setJavaScriptEnabled () method on the WebSettings object. WebViewClient is called when page content is being rendered.

How do I enable JavaScript on a webviewclient?

To use JavaScript, you need to enable it by calling the setJavaScriptEnabled () method on the WebSettings object. WebViewClient is called when page content is being rendered. You can also intercept the URL loading here (using the shouldOverrideUrlLoading () method).


2 Answers

webview.loadUrl("javascript:functionName(\"" + argument + "\")");
like image 124
Yajneshwar Mandal Avatar answered Sep 22 '22 00:09

Yajneshwar Mandal


Change your line with this..

myWebView.loadUrl("javascript:testEcho('Hello World!')");

try this one might be helpful to you...

like image 22
Vasu Avatar answered Sep 21 '22 00:09

Vasu