简体   繁体   中英

Javascript on Android WebView not working

I have some issues with Android WebView and Javascript. Some of customers of app said that WebView on app is not showing anything. As I checked - its probably not showing javascript at all (whole webpage is loaded in javascript by react).

That my code:

    public void setupWebView(WebView accessWebView) {
    accessWebView.setWebViewClient(new WebViewClient() {

        @SuppressWarnings("deprecation")
        @Override
        public boolean shouldOverrideUrlLoading(WebView webView, String url) {
            handleRedirect(accessWebView);
            return true;
        }

    });
    accessWebView.getSettings().setJavaScriptEnabled(true);
    accessWebView.getSettings().setDomStorageEnabled(true);
    accessWebView.loadUrl(URL);

(I have to use WebViewClient, not WebChromeClient, because of the redirect handling)

Is there anything possible to change so the javascript will load on EVERY device with Android +5.0? Is it possible that updating WebView on device will help some users?

You need to use setWebChromeClient to enable javascript in your WebView. But don't worry, you can use both setWebChromeClient and setWebViewClient in the same time. Just like in official docs:

  // Let's display the progress in the activity title bar, like the
  // browser app does.
  getWindow().requestFeature(Window.FEATURE_PROGRESS);

  webview.getSettings().setJavaScriptEnabled(true);

  final Activity activity = this;
  webview.setWebChromeClient(new WebChromeClient() {
    public void onProgressChanged(WebView view, int progress) {
      // Activities and WebViews measure progress with different      scales.
      // The progress meter will automatically disappear when we reach 100%
      activity.setProgress(progress * 1000);
    }
  });
  webview.setWebViewClient(new WebViewClient() {
    public void onReceivedError(WebView view, int errorCode, String      description, String failingUrl) {
      Toast.makeText(activity, "Oh no! " + description,      Toast.LENGTH_SHORT).show();
    }
  });

  webview.loadUrl("https://developer.android.com/");

https://developer.android.com/reference/android/webkit/WebView.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM