0
WebView myWebView = (WebView) findViewById(R.id.webview);                
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setPluginsEnabled(true);        
    myWebView.loadUrl("file:///android_asset/index.html");
    myWebView.loadUrl("javascript:hello()");

It doesnt work in 4.0 emulator or real device. I have proper tag in html like

<script type="text/javascript">..</script>

and i can fire it from firefox's console with no problem (it doesnt call alert command, i know it doesnt work in android)

I am out of solutions, please help me.

Btw this function call seems useless here because it is for testing, i will give a parameter later if this works.

Edit:

<h1 id="title"></h1>
....

<script type="text/javascript">
function hello()
{
  console.log("hello"); 
  var title = document.getElementById("title");
  title.innerHTML = "hello";    
}
</script>
5
  • 1
    What do you want the javascript to do? How do you tell it's not calling the hello()-function? Commented Sep 5, 2012 at 8:51
  • Also, are you trying to call methods in Android, from the javascript? That would require using the addJavascriptInterface()-method on the WebView. Commented Sep 5, 2012 at 8:54
  • It is a test function, it should set an element's value but it doesnt. I know js code is problem free because there is another button in the same html that fires same function without problem. Also logcat warns me about an error: Uncaught referenceerror: hello is not defined at null:1 Commented Sep 5, 2012 at 8:55
  • No i can call Android methods without a problem, i need to call a function in the html that i loaded. Commented Sep 5, 2012 at 8:56
  • Can you provide the element and the simple javascript code you're trying to use? Commented Sep 5, 2012 at 9:04

1 Answer 1

2

The problem is that there is no guarantee that the page has been loaded when you call the javascript. Make sure that you call the javascript function only when the page is completely loaded.

The following code (quoted from http://lexandera.com/2009/01/injecting-javascript-into-a-webview/) will give you a heads up.

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

Thanks so much man, i didnt count on synchronisation because this is not webprogramming but damn.
This doesn't work when I call javascript function in android onClick(){}. Any way to do that ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.