【问题标题】:JS ReferenceError on Android WebView for api level 15用于 API 级别 15 的 Android WebView 上的 JS ReferenceError
【发布时间】:2016-12-26 13:29:47
【问题描述】:

我正在尝试使用 WebView 为 Android 创建一个 Web 应用程序。我的cenary很简单,我有一个带有按钮的index.html文件:

<button id="connect-button" onclick="connect">Connect</button>

和一个简单的(脚本)元素:

<script>
connect = function () {
   alert("Oi");
};
</script>

在 Android 端,我有一个 Activity 可以在 Webview 上加载此文件

WebView web;
web = (WebView) findViewById(R.id.webview01);

//ProgressBar related to the xml
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
progressBar.setMax(100);

web.setWebViewClient(new myWebClient());
web.setWebChromeClient(new MyWebViewClient());
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setLoadWithOverviewMode(true);
web.getSettings().setUseWideViewPort(true);
web.loadUrl("file:///android_asset/index.html");

对于 19 级或更高级别的 API,一切正常。但是对于 api 级别 15 到 18 的 webview 无法加载我的简单 javascript 方法“连接”。

我搜索了很多关于 webview 的 api level bellow 19 的问题,但这是一个非常简单的测试。有些人有时会做一些类似于 api 15 的东西?

【问题讨论】:

    标签: android webview android-4.0-ice-cream-sandwich


    【解决方案1】:

    已解决

    就我而言,问题在于显然 WebView 的 API 低于 19,您必须将任何代码明确地放在“窗口”对象上。

    所以,当我将我的 java 脚本更改为

    <script>
    window.connect = function () {
       window.alert("Oi");
    };
    </script>
    

    一切正常!

    我们有很多类似的问题,如果这对您不起作用,请检查:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-09
      • 1970-01-01
      • 2013-02-13
      相关资源
      最近更新 更多