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+"');");
}
});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With