4

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+"');"); 
                        }

                    });
0

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hello Paul, what if I have to do a little more logic than just injecting the JS right away. Would it potentially require temporarily preventing other loadUrls if I had to iterate through an array of items in a function which injected a javascript line for each item? Or will a webview wait until all of the onPageFinished() logic has terminated before loading another URL?
I would instead apply a JavaScript interface to inform Android what's going on inside the WebView, as suggested by Suhas K.
1

two way communication between JavaScript and your application is possible through WebView.addJavascriptInterface(). Without adding javascript interface its not possible to call javascript function. Check this example: http://android-developers.blogspot.com/2008/09/using-webviews.html

3 Comments

yes its not possible but only if you are developing app for android 4.0+ i think...
No This API is supported on all versions of android.
But it's very unstable on 2.3
0

If you are getting a Error saying that any javascript function or variable is not available in android webview then the issue is that you are trying to run/call java script call before it loads on webview. So, you need to wait for time till the javascript code is loaded in webview. Below snipet solved the issue

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

      });

   //Kotlin code
   webView.webViewClient = object : WebViewClient() {
        override fun onPageFinished(view: WebView, url: String) {
            webView.loadUrl("javascript:Android.functionNamel();")
        }
    }

Comments

-1

I don't think JS function will work in normal default browser, i think you need to ponegap tool for

displaying JS in webview

1 Comment

No it worked, and JS functions work in default android WebView , see update in my question how it worked for me following Paul Lammertsma's suggestion..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.