JavaScript warning does not work in Android WebView

In my application I use WebView and in that I use the JavaScript alert( ) method, but it does not work, the popup does not appear.

in my manifest file i added

 <uses-permission android:name="android.permission.INTERNET"></uses-permission> 

and in the activity file I added

 mWebView = (WebView) findViewById(R.id.webview); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.loadUrl("file:///android_asset/demo.html"); 

In the layout of the xml file, I added

 <WebView android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> 

Any clue how to enable full JavaScript in WebView ?




Update

Thank you, the sign alert() method in the html file now works :).

Now in WebView there are two problems:
1: I use <textarea> in the html file that I upload to WebView and try to write the Hindi font in it, but when I try to write Hindi text, it displays as characters (rectangle characters like []).

when I do the same in the Firefox browser on the desktop, it works fine. any clue how to give support for multiple languages ​​in textarea in WebView ?

2: When I click the "Send" button and try to open the text value in the alert() method in another java script, this does not work, does this mean even after using WebChromeClient it is applicable only to the current loaded html page, and not javascripts called with this page?

+72
javascript android
Mar 11 2018-11-11T00:
source share
6 answers

Check out this link and last comment. You should use WebChromeClient for your purpose.

+81
Mar 11 2018-11-11T00:
source share

As others have pointed out, setting up WebChromeClient is necessary for alert() work. Just set WebChromeClient () by default:

 mWebView.getSettings().setJavaScriptEnabled(true); mWebView.setWebChromeClient(new WebChromeClient()); 

Thanks for all the comments below. Including John Smith, who indicated that you need to enable JavaScript.

+96
Sep 25 '12 at 4:17
source share
 webView.setWebChromeClient(new WebChromeClient() { @Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { return super.onJsAlert(view, url, message, result); } }); 
+18
Feb 20 '13 at 1:53
source share

The following code will work:

 private WebView mWebView; final Activity activity = this; // private Button b; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mWebView = (WebView) findViewById(R.id.webview); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.getSettings().setDomStorageEnabled(true); mWebView.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view, int progress) { activity.setProgress(progress * 1000); } }); mWebView.loadUrl("file:///android_asset/raw/NewFile1.html"); } 
+5
Sep 04
source share

You can try with this, it worked for me

 WebView wb_previewSurvey=new WebView(this); wb_previewSurvey.setWebChromeClient(new WebChromeClient() { @Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { //Required functionality here return super.onJsAlert(view, url, message, result); } }); 
0
Aug 28 '17 at 11:52 on
source share

Just add

 mWebview.setWebChromeClient(new WebChromeClient()); 

This will work.

0
Apr 15 '18 at 15:44
source share



All Articles