Home >Web Front-end >JS Tutorial >How to Call JavaScript Functions from an Android WebView?
It is possible to invoke JavaScript functions from within an Android WebView using the addJavascriptInterface() method.
To enable this functionality, ensure that JavaScript is enabled on the WebView:
myWebView.getSettings().setJavaScriptEnabled(true);
Additionally, register a Java class that contains the methods you want to expose to JavaScript:
myWebView.addJavascriptInterface(myJSInterface, "JSInterface");
To call a JavaScript function from the Android app, use the following syntax:
myWebView.loadUrl("javascript:testEcho(Hello World!)");
It turns out that the provided code had a missing quote in the parameter of the testEcho() function call. The correct approach is:
myWebView.loadUrl("javascript:testEcho('Hello World!')");
This resolves the issue and allows the JavaScript function to be successfully invoked from the Android app.
The above is the detailed content of How to Call JavaScript Functions from an Android WebView?. For more information, please follow other related articles on the PHP Chinese website!