Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android WebView Unable to find JavaScript function

i am trying to call JS function from android activity it loads the page in webview but couldn't find out the JS function , the function is working very fine in desktop browser

Here is the JAVA Code

setting almost each and everything for JS

    index = (WebView) findViewById(R.id.edit);
    WebSettings Websettings = index.getSettings();
    Websettings.setBuiltInZoomControls(true);
    Websettings.setSupportZoom(true);  
    Websettings.setJavaScriptEnabled(true);
    Websettings.setBuiltInZoomControls(true); 
    Websettings.setRenderPriority(RenderPriority.HIGH);
    index.getSettings().setPluginState(PluginState.ON);
    index.setWebChromeClient(new WebChromeClient());

and here is how i am calling JS function

index.loadUrl("file:///android_asset/demo.html");
index.loadUrl("javascript:getList('"+JsonString+"');"); 

Here is the JS function

function getList(jString) 
{
alert('function called');
}

Here is what the LogCat Says

04-24 16:44:58.015: E/Web Console(31516): Uncaught ReferenceError: 
getList is not defined:1

Update: Solution:-

As Paul Lammertsma sugessted, i was not waiting for html page to load fully and was calling the JS function rite after loading html page in webView. so by calling JS function in onPageFinished solved my problem this worked for me:

index.setWebViewClient(new WebViewClient() {
                        @Override
                        public void onPageFinished(WebView view, String url) {
                            index.loadUrl("javascript:getList('"+JsonString+"');"); 
                        }

                    });
like image 857
Addy Avatar asked Sep 07 '25 04:09

Addy


1 Answers

Calling loadUrl() is asynchronous; the JavaScript probably hasn't loaded yet when you invoke it for executing the JavaScript function. Perhaps you want to invoke the function when the page has finished loading.

like image 95
Paul Lammertsma Avatar answered Sep 10 '25 09:09

Paul Lammertsma