【问题标题】:Android Webview Uncaught ReferenceError: $ is not definedAndroid Webview Uncaught ReferenceError: $ is not defined
【发布时间】:2023-04-01 02:57:01
【问题描述】:

我想在 Webview 中显示简单的引导 Jquery 的引导表。

在桌面浏览器上运行良好,但在 webview 的 Chrome 调试器中会抛出以下异常。

Android Webview Uncaught ReferenceError: $ is not defined

Webview的设置方式如下:

public void setWebview() {
    try {
        AssetManager mgr = getBaseContext().getAssets();
        mWebView = (WebView) findViewById(R.id.intro_browser);

        InputStream in = mgr.open("www/table.html", AssetManager.ACCESS_BUFFER);
        String htmlContentInStringFormat = StreamToString(in);
        in.close();

        // Set Chrome instead of the standard WebView
        mWebView.setWebChromeClient(new WebChromeClient());
        mWebView.getSettings().setJavaScriptEnabled(true);
            mWebView.getSettings().setAllowFileAccessFromFileURLs(true);
            mWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
        mWebView.clearCache(true);
        mWebView.clearHistory();
        mWebView.getSettings().setAllowContentAccess(true);
        mWebView.getSettings().setBlockNetworkImage(false);
        mWebView.loadDataWithBaseURL(null, htmlContentInStringFormat, "text/html", "utf-8", null);
    } catch (Exception e) {
        Log.d("TEST", e.getMessage());
    }

    }

assets/www 文件夹中的 HTML 如下:

<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Table Fixed Header</title>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.min.css">

    <!-- Optional theme -->
    <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap-theme.min.css">

    <!-- Latest compiled and minified JavaScript -->
    <script src="bower_components/jquery/src/jquery-2.1.4.min.js"></script>

    <!-- Latest compiled and minified JavaScript -->
    <script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>


    <style>
        .table-responsive>.fixed-column {
          position: absolute;
          display: inline-block;
          width: auto;
          border-right: 1px solid #ddd;
        }
        @media(min-width:768px) {
          .table-responsive>.fixed-column {
            display: none;
          }
        }

        /*Heading */

    </style>

</head>
<body>
<div class="table-responsive">
    <table class="table table-striped table-bordered table-hover table-condensed header-fixed">
        <thead>
        <tr>
            <th>#</th>
            <th>Table heading</th>
            <th>Table heading</th>
            <th>Table heading</th>
            <th>Table heading</th>
            <th>Table heading</th>
            <th>Table heading</th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td>3</td>
            <td>Table cell</td>
            <td>Table cell</td>
            <td>Table cell</td>
            <td>Table cell</td>
            <td>Table cell</td>
            <td>Table cell</td>
        </tr>
        </tbody>
    </table>
</div>
<script>
$( document ).ready(function() {
    console.log( "ready!" );
    var $table = $('.table');
    var $fixedColumn = $table.clone().insertBefore($table).addClass('fixed-column');

    $fixedColumn.find('th:not(:first-child),td:not(:first-child)').remove();

    $fixedColumn.find('tr').each(function (i, elem) {
      $(this).height($table.find('tr:eq(' + i + ')').height());
    });

    //ALert is not called in the Webview
    alert("TEST");
});

    </script>
</body>
</html>

请问我该如何解决?非常感谢您的任何建议。

【问题讨论】:

  • 如果android app中有bower_components,那么依赖是怎么安装的,bower install
  • 是的,但这仅用于测试目的。
  • 我也有同样的问题 :(
  • 同样的问题。请分享解决方案。 :(

标签: javascript android jquery twitter-bootstrap webview


【解决方案1】:

尝试将您的 UA(用户代理)更改为

Mozilla/5.0 (Linux; Android 4.1.1; HTC One X Build/JRO03C) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.58 Mobile Safari/537.31

在我的情况下,我正在更改 UA 并且出现了问题。当我更改为这个值时,一切正常。

我在以下位置找到了这个:

https://github.com/jitsi/jitsi-meet/issues/362

【讨论】:

  • 另一个选项是:Mozilla/5.0 (Linux; Android 5.1; XT1021 Build/LPCS23.13-34.8-3; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/55.0。 2883.91 移动版 Safari/537.36
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-08
  • 2019-01-22
  • 2019-12-31
相关资源
最近更新 更多