How we can execute a javascript function and get a return value in our android appplication ?
We want to execute a javascript on a button press event, we need to pass parameters to the script and get return values, So we are using WebChromeClient to implement this,
But we got Exception is SyntaxError: Parse error at undefined:1
Following is my code
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class FirstTab extends Activity
{
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.regis);
try{
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new MyWebChromeClient());
String customHtml = <html><head><title>iSales</title><script type=text/javascript> function fieldsOnDelete(){ var x=123; return JIJO; } </script></head><body>hi</body></html>;
webView.loadData(customHtml, text/html,UTF-8);
}catch(Exception e)
{
Log.v(JAC LOG,e.toString());
}
}
public void onResume()
{
super.onResume();
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try{
webView.loadUrl(javascript:alert(javascript:fieldsOnDelete()));
}
catch(Exception e)
{
Log.v(JAC LOG,e.toString());
}
}
});
}
final class MyWebChromeClient extends WebChromeClient {
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
Log.v(LogTag, message);
result.confirm();
return true;
}
}
}