user2078588
user2078588

Reputation: 81

Adding javascript code after a remote html page loaded in Android Webview

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

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

For example, after loading in the webview:

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

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

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

But the alert is not displayed.

Thanks for any feedback, and sorry for my not perfect English.

Upvotes: 8

Views: 8622

Answers (3)

Joy Hard
Joy Hard

Reputation: 319

Try this:

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 link of source!

Upvotes: 9

Hassan Karimi
Hassan Karimi

Reputation: 180

this work for me

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

Upvotes: 0

lgvalle
lgvalle

Reputation: 31

alert() is a bit tricky. You need to capture it and transform it to a Toast.

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

Upvotes: 0

Related Questions