Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
3  
The code you try to use misses quotes on the javascript side. – Paul de Vrieze Mar 7 '11 at 14:40

5 Answers

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!')");
share|improve this answer
please have a look in these link stackoverflow.com/questions/9079374/… – kamal_tech_view Feb 2 '12 at 5:59
public void run(final String scriptSrc) { 
        webView.post(new Runnable() {
            @Override
            public void run() { 
                webView.loadUrl("javascript:" + scriptSrc); 
            }
        }); 
    }
share|improve this answer

Yes you have the syntax error. If you want to get your Javascript errors and printing statements in your logcat you must implement the onConsoleMessage(ConsoleMessage cm) in your WebChromeClient. It gives the complete stack traces like Web console(Inspect element). Here is the method.

public boolean onConsoleMessage(ConsoleMessage cm) 
    {
        Log.d("ShowMote", cm.message() + " -- From line "
                             + cm.lineNumber() + " of "
                             + cm.sourceId() );
        return true;
    }

After implementation you will get your Javascript errors and print statements*(console.log)* on your logcat.

share|improve this answer
2  
I believe the is on the WebChromeClient and not the WebViewClient. – Michael Levy Feb 4 at 15:26
Yes you are correct @MichaelLevy. Thanks for pointing me for that mistake. Now I have edited my answer. – Dinesh Feb 7 at 6:42
Thanks, your answer was a just what I needed. I'd also suggest people override onJsAlert() in the WebChromeClient. The combination of javascript logging and alerts can be really helpful when debugging Javascript hosted in the webview. – Michael Levy Feb 7 at 16:54
1  
(Where do people get the idea of randomly bolding parts of messages? It sure makes it hard to read.) – Glenn Maynard Jun 3 at 15:48

This video (http://youtu.be/uVqp1zcMfbE) gave me the hint to make it work. The key is to save your HTML and JS files in the Android assets folder. Then you can easily access them via:

webView.loadUrl("file:///android_asset/your_page.html");
share|improve this answer

Use addJavaScriptInterface("nameOfObject",object) - Link

share|improve this answer
2  
This is the wrong answer. Question is about "trying to call some javascript functions... running inside an android webview", not for "Java object's public methods to be accessed from JavaScript". At least get the order of the arguments right. – dragon Nov 2 '12 at 23:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.