1

Hi In my android phonegap app i need to call the javascript method from android code (DroidGap).I have tried the sample code.

Here is my code:

super.loadUrl("file:///android_asset/www/index.html");
super.loadUrl("javascript:onload()");

When i use super.loadUrl("javascript:alert('hai')"); i am getting this alert.But when i use the method "onload" i am getting the error.

Here is my error in logcat:

Uncaught TypeError: Property 'onload' of object [object DOMWindow] is not a function at null:1

Here is my script in index.html:

 <script type="text/javascript">
 function onload()
 {
     alert("hai");
 }
 </script>

I dont know where i am wrong.Please guide me.Thanks in Advance.

2

4 Answers 4

1

Try this one and add this line also

    super.setWebChromeClient(new WebChromeClient());
    super.loadUrl("file:///android_asset/www/index.html");

After this line call like this onPageFinished

webview.setWebViewClient(new WebViewClient() {                      
  @Override
    public void onPageFinished(WebView view, String url)  {     

       webview.loadUrl("javascript:(function() {alert("hai") }
      );                         
   }                
});                     
Sign up to request clarification or add additional context in comments.

4 Comments

I am using Droidgap so i used super.loadUrl
I have tried your code but i cannot able to super instead i used webview.Now i didnt get any error but no use,alert is not coming.Please guide me.
here is example try this polamreddyn.blogspot.in/2013/05/…
0

Android can only call the javascript method if an html page is currently loaded in webView

first call

webview.loadUrl("Your html page url"); then call

webView.loadUrl("javascript:hello()");

Comments

0

Try to handle the alert function in the java file, like this :

mWebView.setWebChromeClient(new MyWebChromeClient());    

    final class MyWebChromeClient extends WebChromeClient {        

        @Override       
        public boolean onJsAlert(WebView view,String url,        
                                 String message,JsResult result) {        

            new AlertDialog.Builder(MainActivity.this).       
                setTitle("Alert").setMessage(message).setPositiveButton("OK",       
           new DialogInterface.OnClickListener() {       
                    @Override      
                    public void onClick(DialogInterface arg0, int arg1) {       

                   }       
            }).create().show();       
           result.confirm();        
            return super.onJsConfirm(view,url,message, result);        
        }    

    }

Comments

0

I had the same problem when I moved the target javascript function from the main page to an separate .js file. For some reason, the loadUrl() function can't find externally-loaded functions - only the ones in the main page. Once I moved the function back, it immediately started working.

Go figure.

Comments

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.