Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8875867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:05:33+00:00 2026-06-14T19:05:33+00:00

How we can execute a javascript function and get a return value in our

  • 0

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;
        }
    }


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:05:34+00:00Added an answer on June 14, 2026 at 7:05 pm

    you can use mWebView.loadUrl("javascript:checkName"); to call the method…

    Then you can use addJavascriptInterface() to add a Java object to the Javascript environment. Have your Java script call a method on that Java object to supply its “return value”.

    EDIT1: Or you can use following hack:

    Add this Client to your WebView:

    final class MyWebChromeClient extends WebChromeClient {
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                Log.d("LogTag", message);
                result.confirm();
                return true;
            }
        }
    

    Now in your java script call do:

    webView.loadUrl("javascript:alert(functionThatReturnsSomething)");
    

    Now in the onJsAlert call “message” will contain the returned value.

    Edit2:

    So it does not work if we call javascript method just after call to load the URL since the page loads take time. So I created a test program to test that…

    Following is my html file (named test.html) store in the assets folder:

    <html>
    <head>
    <script language="javascript">
        function fieldsOnDelete(message) {
            alert("i am called with " + message);
            window.myjava.returnValue(message + " JIJO");
        }
    </script>
    <title>iSales android</title>
    
    
    </head>
    <body></body>
    </html>
    </body>
    </html>
    

    Following is my java class that would get that i would add to java script as interface and it would receive the return value:

    public class MyJS {
    
        public void returnValue(String string){
            Log.d(this.getClass().getSimpleName(), string);
        }
    
    }
    

    And following is my activity class:

    public class CheckWebView extends Activity {
    
        private WebView webView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_check_web_view);
            webView = (WebView) findViewById(R.id.webview);
    
            webView.setWebChromeClient(new WebChromeClient() {
                @Override
                public void onConsoleMessage(String message, int lineNumber,
                        String sourceID) {
                    super.onConsoleMessage(message, lineNumber, sourceID);
                    Log.d(this.getClass().getCanonicalName(), "message " + message
                            + "   :::line number " + lineNumber + "   :::source id "
                            + sourceID);
                }
    
                @Override
                public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
                    // TODO Auto-generated method stub
    
                    onConsoleMessage(consoleMessage.message(),
                            consoleMessage.lineNumber(), consoleMessage.sourceId());
    
                    Log.d(this.getClass().getCanonicalName(), "message::::: "
                            + consoleMessage.message());
    
                    return super.onConsoleMessage(consoleMessage);
                }
            });
    
            webView.addJavascriptInterface(new MyJS(), "myjava");
            webView.getSettings().setJavaScriptEnabled(true);
            webView.getSettings().setPluginsEnabled(true);
            webView.getSettings().setAllowFileAccess(true);
    
            webView.loadUrl("file:///android_asset/test.html");
    
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_check_web_view, menu);
            return true;
        }
    
        /* (non-Javadoc)
         * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
         */
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            webView.loadUrl("javascript:fieldsOnDelete('name');");
            return super.onOptionsItemSelected(item);
        }
    
    }
    

    The key here is that there should be some time interval between the call to load html file from assets folder and the call to javascript:method. Here I am calling it from onOptionsItemSelected and it is working fine.. if I move the webView.loadUrl("javascript:fieldsOnDelete('name');"); to the end of the onCreate() method the it shows the error that it can not find fieldsOnDelete() method…

    Hope it Helps…

    EDIT3:

    Replace following in your code

    webView.loadUrl("javascript:alert(javascript:fieldsOnDelete())");
    

    with

    webView.loadUrl("javascript:alert(fieldsOnDelete())");
    

    and try…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can I pass some Javascript to a function and then execute that Javascript from
How can I set a delayed trigger in JavaScript to execute a function after
Can I create an event so I can execute some javascript whenever an element
How I can execute a JavaScript function/piece of code in a specific context which
My HTML file has a javascript function xxx_return(), which will return a string value.
Can I call javascript function from MVC controller action (not from view page) and
In node.js vm module , i can execute some javascript in another node.js process.
When I execute this JavaScript file in Firefox; <script type=text/javascript > $(function () {
So, here's some sample javascript code: Object.prototype.simpleFunction = function () { return true; }
I'm trying to dynamically load a Javascript based on the return value of an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.