Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 757
user2078588 Avatar asked Feb 16 '13 15:02

user2078588


People also ask

How do I enable JavaScript on Android WebView?

JavaScript is disabled in a WebView by default. You can enable it through the WebSettings attached to your WebView . You can retrieve WebSettings with getSettings() , then enable JavaScript with setJavaScriptEnabled() . WebView myWebView = (WebView) findViewById(R.

Can WebView run JavaScript?

WebView is a special component in Android which serves as kind of built-in browser inside Android applications. If you want to execute HTML, CSS or JavaScript code in your Android app, or you need to allow users visit a URL without leaving your application, WebView is the way to go.

Is it possible to get data from HTML forms into Android while WebView?

Yes you can, you can use javascript to get webpage content. Then use the webview jsInterface to return the content to you java code.


1 Answers

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!

like image 185
Joy Hard Avatar answered Oct 12 '22 19:10

Joy Hard