简体   繁体   English

Android WebView 上的 Javascript 不起作用

[英]Javascript on Android WebView not working

I have some issues with Android WebView and Javascript.我对 Android WebView 和 Javascript 有一些问题。 Some of customers of app said that WebView on app is not showing anything.一些应用程序客户表示应用程序上的 WebView 没有显示任何内容。 As I checked - its probably not showing javascript at all (whole webpage is loaded in javascript by react).正如我检查的那样 - 它可能根本没有显示 javascript(整个网页都是通过 react 加载到 javascript 中的)。

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) (由于重定向处理,我必须使用 WebViewClient,而不是 WebChromeClient)

Is there anything possible to change so the javascript will load on EVERY device with Android +5.0?有什么可以改变的,所以 javascript 将加载到每个 Android +5.0 的设备上? Is it possible that updating WebView on device will help some users?在设备上更新 WebView 是否可能对某些用户有所帮助?

You need to use setWebChromeClient to enable javascript in your WebView.您需要使用 setWebChromeClient 在您的 WebView 中启用 javascript。 But don't worry, you can use both setWebChromeClient and setWebViewClient in the same time.不过别担心,你可以同时使用 setWebChromeClient 和 setWebViewClient 。 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 https://developer.android.com/reference/android/webkit/WebView.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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