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
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);
0
source to share