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.

Sudha
  • 505
  • 1
  • 10
  • 27
  • possible duplicate of [JavaScript alert not working in Android WebView](http://stackoverflow.com/questions/5271898/javascript-alert-not-working-in-android-webview) – Pragnani May 08 '13 at 11:42
  • here is working example try this http://polamreddyn.blogspot.in/2013/05/php-alert-dialog-in-webview.html – NagarjunaReddy May 08 '13 at 14:03

4 Answers4

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") }
      );                         
   }                
});                     
NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98
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()");

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

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

Michael Galaxy
  • 1,213
  • 14
  • 17