【问题标题】:Javascript on Android WebView not workingAndroid WebView上的Javascript不起作用
【发布时间】:2017-10-19 17:34:48
【问题描述】:

我在使用 Android WebView 和 Javascript 时遇到了一些问题。 一些应用程序的客户说应用程序上的 WebView 没有显示任何内容。 正如我所检查的 - 它可能根本没有显示 javascript(整个网页通过反应加载到 javascript 中)。

我的代码:

    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);

(由于重定向处理,我必须使用 WebViewClient,而不是 WebChromeClient)

有什么可以改变的,所以 javascript 将在每台 Android +5.0 的设备上加载? 更新设备上的 WebView 是否可能对某些用户有所帮助?

【问题讨论】:

    标签: android webview


    【解决方案1】:

    您需要使用 setWebChromeClient 在您的 WebView 中启用 javascript。不过不用担心,您可以同时使用 setWebChromeClient 和 setWebViewClient 。就像在官方文档中一样:

      // 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

    【讨论】:

    • 感谢您提供的信息。不幸的是,在那种情况下它没有帮助。我认为这可能是在 webview 中加载的那个网站的问题。它完全用 react 编写,可以使用仅在较新的 WebView 上可用的方法。所以现在,我建议更新 WebView 应用程序。
    猜你喜欢
    • 2018-11-09
    • 1970-01-01
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    • 2017-10-06
    • 2014-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多