2

I have already found many examples about how to call JavaScript from android. But it's not working for me. My target SDK is 17(android 4.2). This is how I am loading my html page from my activity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);

    myWebView = (WebView)findViewById(R.id.mapwebview1);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    JavaScriptHandler jScriptHandler = new JavaScriptHandler(this); 
    WebChromeClient myWebChromeClient = new WebChromeClient();
    myWebView.setWebChromeClient(myWebChromeClient);
    myWebView.addJavascriptInterface(jScriptHandler, "MyHandler");

    myWebView.loadUrl("file:///android_asset/mywebpage.html");
    myWebView.loadUrl("javascript:myFunc()");


}

Here is the code for my JavaScriptHandler:

public class JavaScriptHandler {
//TabFragmentMap mapFragment;
Context context;
//Fragment fragment;

public JavaScriptHandler (Context c){
    this.context = c;
}

}

Here is the code for my html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>PhoneGap</title>
</head>
<body onload="myFunc()">
<h1 id="test1">Hello World</h1>
<input type="button" value="Say hello" onClick="moveMyself()" />
<div id="myDiv"></div>
<script type="text/javascript"> 


    function myFunc()
    {

       document.getElementById('test1').innerHTML = 'Good Morning';

    }

</script>
</body>
</html>
1
  • did you had a chance to refer this link. I hope this would be helpful for you. Commented Mar 18, 2014 at 7:10

3 Answers 3

2

Try this:

final WebView webview = (WebView)findViewById(R.id.browser);  
/* JavaScript must be enabled if you want it to work, obviously */  
webview.getSettings().setJavaScriptEnabled(true);  

/* WebViewClient must be set BEFORE calling loadUrl! */  
webview.setWebViewClient(new WebViewClient() {  
    @Override  
    public void onPageFinished(WebView view, String url)  
    {  
        webview.loadUrl("javascript:(function() { " +  
                "document.getElementsByTagName('body')[0].style.color = 'red'; " +  
                "})()");  
    }  
});  

webview.loadUrl("http://code.google.com/android");  
Sign up to request clarification or add additional context in comments.

2 Comments

wow..this is working. Great effort. Thanks for your help. Now I have to call an already existing JavaScript function. Please give me some hint if you know anything.
I could finally make it. Thanks a lot for your great help. Please see below my answer how to do it.
2

It was actually the same thing that Tamilarasi has given me. If somebody wants to call an existing JavaScript function from the html, do the following:

myWebView.setWebViewClient(new WebViewClient() {
        public void onPageFinished(WebView view, String url){
            myWebView.loadUrl("javascript:myFunc()");
        }
});
myWebView.loadUrl("file:///android_asset/myHtml.html");

Comments

-1

Try this links i hope this will be help to u:

http://android-er.blogspot.in/2011/10/call-javascript-inside-webview-from.html

Android 4.2.1, WebView and javascript interface breaks

Javascript interface not working with android 4.2

6 Comments

Thanks for your answer. But these links is nothing different from what I am doing. It is not helping me :(
I could call a android function from html JavaScript. But I can't do the other way: from android to JavaScript :(
The 2nd and 3rd link that you have provided are about JavaScript -> Android interaction. I am successful with this. So, if a JavaScript function is executed, it is calling an Android function properly. But I need to pass data to html page via the JavaScript function. I am stuck with this issue.
will you need like your enter the text into textbox the textbox text is diplay the javascript dialog box when you click the button
If you see my code, I want to call the JavaScript function named myFunc() inside my activity's onCreate(). But it is not being called :(. You can see similar example at the following link:blog.objectgraph.com/index.php/2012/03/16/…
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.