Enabling Common JavaScript in WebViewClient

When searching for an answer on google, it seems that I'm not the only one who has a problem that seems impossible to solve.

I was able to create a WebView with a custom WebViewClient - this allows me to have a processdialog and show an error message if the URL cannot be loaded.

But this creates a problem with JavaScript. The URL I'm loading contains JavaScript that changes some HTML CSS styles (showing or hiding the element) or redirects them to another onclick location - or maybe even wants to show a warning window. But using WebViewClient none of them work.

This is how I load the page:

public void loadUrl(String url) { final ProgressDialog dialog = ProgressDialog.show(myActivity.this, "", getString(R.string.loading), true); final WebView myWebView = (WebView) findViewById(R.id.webview); myWebView.setVerticalScrollBarEnabled(false); myWebView.setHorizontalScrollBarEnabled(false); WebSettings webSettings = myWebView.getSettings(); webSettings.setJavaScriptEnabled(true); myWebView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { Toast.makeText(myActivity.this, url, Toast.LENGTH_SHORT).show(); //Debugging purposes if (url.endsWith(".mp4")) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.parse(url), "video/mp4"); view.getContext().startActivity(intent); } else { view.loadUrl(url); } return true; } public void onPageFinished(WebView view, String url) { //Toast.makeText(myActivity.this, "Oh no!", Toast.LENGTH_SHORT).show(); dialog.dismiss(); } public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { Toast.makeText(myActivity.this, description, Toast.LENGTH_SHORT).show(); String summary = "<html><body><strong>" + getString(R.string.lost_connection) + "</body></html>"; myWebView.loadData(summary, "text/html", "utf-8"); } }); //End WebViewClient myWebView.loadUrl(url); } 

This could probably have been done more reasonably, but I'm new to Java and Android development ...

Is it possible for me to enable JavaScript for WebViewClient? Removing WebViewClient solves the problem, but then I can not catch events when the page fails or the download has finished.

+61
android webview webviewclient
Feb 23 '11 at 10:23
source share
6 answers

I don't know what your exact problem is, but I can run JavaScript and a custom WebViewclient without any problems:

 WebView vistaWeb = (WebView) findViewById(R.id.webview); vistaWeb.setWebChromeClient(new MyCustomChromeClient(this)); vistaWeb.setWebViewClient(new MyCustomWebViewClient(this)); vistaWeb.clearCache(true); vistaWeb.clearHistory(); vistaWeb.getSettings().setJavaScriptEnabled(true); vistaWeb.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 
+125
Feb 23 2018-11-11T00:
source share

Try enabling javascript

 WebView myWebView = (WebView) findViewById(R.id.webView); myWebView.getSettings().setJavaScriptEnabled(true); myWebView.setWebViewClient(new WebViewClient()); myWebView.loadUrl(url); 
+4
Oct 03 '17 at 13:05
source share

The proper way to enable JavaScript is to add the following two lines:

 mWebView.getSettings().setJavaScriptEnabled(true); mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 

Even if after adding does not work, try adding the line below.

 mWebView.getSettings().setDomStorageEnabled(true); 

Now it should work. :)

+4
Jul 20 '18 at 15:17
source share

Do "Javascript URLs" shouldOverrideUrlLoading through shouldOverrideUrlLoading ? Try checking this, and if so, return false for such links (so that the webview processes the link, not your WebViewClient)

+1
Feb 23 2018-11-11T00:
source share

What happened in my case: I served local HTML files and when applying

  web.getSettings().setJavaScriptEnabled(true); web.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 

but then my urls stopped working. The solution to make JS and local href work was to add

  web.getSettings().setAllowFileAccess(true); web.getSettings().setAllowFileAccessFromFileURLs(true); 

Where

  web = (WebView) findViewById(R.id.embedded_web); 
+1
Feb 15 '18 at 17:13
source share

Looks like @mdelolmo's answer, but in Kotlin:

  webview.setWebChromeClient(WebChromeClient()) webview.setWebViewClient(WebViewClient()) webview.clearCache(true) webview.clearHistory() webview.getSettings().setJavaScriptEnabled(true) webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true) 
0
May 6 '19 at 8:52
source share



All Articles