Android Calling JavaScript functions in WebView
-
I am trying to call some javascript functions sitting in an html page running inside an android webview. Pretty simple what the code tries to do below - from the android app, call a javascript function with a test message, which inturn calls a java function back in the android app that displays test message via toast. The javascript function looks like: function testEcho(message){ window.JSInterface.doEchoTest(message); } From the WebView, I have tried calling the javascript the following ways with no luck: myWebView.loadUrl("javascript:testEcho(Hello World!)"); mWebView.loadUrl("javascript:(function () { " + "testEcho(Hello World!);" + "})()"); I did enable javascript on the WebView myWebView.getSettings().setJavaScriptEnabled(true); // register class containing methods to be exposed to JavaScript myWebView.addJavascriptInterface(myJSInterface, "JSInterface"); And heres the Java Class public class JSInterface{ private WebView mAppView; public JSInterface (WebView appView) { this.mAppView = appView; } public void doEchoTest(String echo){ Toast toast = Toast.makeText(mAppView.getContext(), echo, Toast.LENGTH_SHORT); toast.show(); } } I've spent a lot of time googling around to see what I may be doing wrong. All examples I have found use this approach. Does anyone see something wrong here? Edit: There are several other external javascript files being referenced & used in the html, could they be the issue?
-
Answer:
I figured out what the issue was : missing quotes in the testEcho() parameter. This is how I got the call to work: myWebView.loadUrl("javascript:testEcho('Hello World!')");
user163757 at Stack Overflow Visit the source
Related Q & A:
- How to stop a webview Android?Best solution by Stack Overflow
- Why is Javascript called Javascript, if it has nothing to do with Java?Best solution by Stack Overflow
- How do I display limited html content in a webview?Best solution by Stack Overflow
- How to detect Android in JavaScript?Best solution by Stack Overflow
- Do piecewise functions have parent functions?Best solution by Ask.Metafilter.Com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.