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>
FireFly
  • 79
  • 9
  • 1
    What do you want the javascript to do? How do you tell it's not calling the `hello()`-function? – Aske B. Sep 05 '12 at 08:51
  • Also, are you trying to call methods in Android, from the javascript? That would require using the `addJavascriptInterface()`-method on the `WebView`. – Aske B. Sep 05 '12 at 08: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 – FireFly Sep 05 '12 at 08:55
  • No i can call Android methods without a problem, i need to call a function in the html that i loaded. – FireFly Sep 05 '12 at 08:56
  • Can you provide the element and the simple javascript code you're trying to use? – Aske B. Sep 05 '12 at 09:04

1 Answers1

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");
Rajesh
  • 15,724
  • 7
  • 46
  • 95
  • Thanks so much man, i didnt count on synchronisation because this is not webprogramming but damn. – FireFly Sep 05 '12 at 09:28
  • This doesn't work when I call javascript function in android onClick(){}. Any way to do that ? – partho Sep 27 '20 at 15:51