Adding javascript code after remote html page loaded in Android Webview

I have a problem with webview in android. I am working with an application that has 2 fragments; first has EditText and second webview.

Is it possible to pass an Edittext string on a page loaded in a webview by calling a javascript function that modifies the DOM of the loaded page?

For example, after loading into webview:

webview.loadUrl("http://google.it");

      

I want to insert into the edittext of google search page a string passed from java; I tried this as the first step:

webview.setWebViewClient(new WebViewClient() {  
            @Override  
            public void onPageFinished(WebView view, String url)  
            {  
               webview.loadUrl("javascript:(function() {alert();})()");  
            }  
        });  

      

But no warning is displayed.

Thanks for any feedback and sorry for my unprepared english.

+3


source to share


3 answers


alert () is a little more complicated. You need to capture it and turn it into a Toast.



Check it out: https://code.google.com/p/apps-for-android/source/browse/Samples/WebViewDemo/src/com/google/android/webviewdemo/WebViewDemo.java

0


source


this work for me



  webview.loadUrl("javascript:function sagvelgardkharast() { document.getElementById('gkFooter').style.display='none'; " +
                        " document.getElementById('gkBottom5').style.display='none'; " + 
                        " } sagvelgardkharast(); ");

      

0


source


Try the following:

webview.getSettings().setJavaScriptEnabled(true);
webview.setWebChromeClient(new WebChromeClient());
webview.setWebViewClient(new WebViewClient() {
   @Override
   public void onPageFinished(WebView view, String url){
       String javaScript ="javascript:(function() {alert();})()";
       webview.loadUrl(javaScript);
   }
});
webview.loadUrl(url);

      

Here is the source link !

0


source







All Articles