javascript - android webview本地html文件与javascript,脚本不起作用

标签 javascript java android html

我正在尝试从 webview 中的 asset 文件夹加载本地 HTML 文件。我可以在 webview 中加载 HTML 文件,但 html 文件依赖于一个外部 .js 文件来进行一些计算。当我在 webview 中单击计算器的功能时,它没有执行任何操作(它没有使用 .js 文件)。

我更改了 HTML 中的路径以匹配 .js 文件的路径:

<script language="JavaScript" src="file:///android_asset/convert2.js" type="text/javascript"></script>

但它仍然不起作用。有什么想法吗?

这是 HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
                                            <html>
                                              <head>
                                                <meta content="text/html; charset=windows-1252"

                                                  http-equiv="content-type">
                                                <script language="JavaScript" src="file:///android_asset/convert2.js" type="text/javascript"></script>
                                                <link href="au_files/style.css"

                                                  rel="stylesheet" type="text/css">
                                                <link href="au_files/print.css"

                                                  rel="stylesheet" type="text/css"

                                                  media="print">
                                              </head>
                                              <body>
                                                <center>
                                                  <table id="mainbg"

                                                    border="0" cellpadding="1" cellspacing="0"

                                                    width="692">
                                                    <tbody>
                                                      <tr>
                                                        <td>
                                                          <!-- The above table places the 1 pixel gray outline around the whole page -->
                                                          <table border="0" cellpadding="0"

                                                            cellspacing="0" width="690">
                                                            <tbody>
                                                              <tr>
                                                                <td valign="top"

                                                                  width="500">
                                                                  <table style="width: 687px; height: 384px;"

                                                                  border="0" cellpadding="10"

                                                                  cellspacing="0">
                                                                  <tbody>
                                                                  <tr>
                                                                  <td class="content"

                                                                  valign="top" width="500">
                                                                  <div align="center">
                                                                  <h1> <strong><span>Astronomical
                                                                  Unit
                                                                  Conversion</span></strong>
                                                                  </h1>
                                                                  </div>
                                                                  <br>
                                                                  <form name="MainForm"

                                                                  align="center">
                                                                  <div align="center">
                                                                  <center>
                                                                  <table border="0">
                                                                  <tbody>
                                                                  <tr>
                                                                  <td style="text-align: center;"

                                                                  colspan="2"><strong>Convert:</strong>
                                                                  <input name="what"

                                                                  size="15" onfocus="javascript:resetanswer();"

                                                                  onkeypress="microsoftKeyPress();"

                                                                  type="text">&nbsp;</td>
                                                                  </tr>
                                                                  <tr>
                                                                  <td align="center">From:<br>
                                                                  <select name="from"

                                                                  size="10" onchange="javascript:myCon();">
                                                                  <option selected="selected"

                                                                  value="149597870691">astronomical
                                                                  unit [1996]</option>
                                                                  <option value="1000">kilometer</option>
                                                                  <option value="299792458">light
                                                                  second</option>
                                                                  <option value="299792458 * 60">light
                                                                  minute</option>
                                                                  <option value="299792458 * 60 * 60">light
                                                                  hour</option>
                                                                  <option value="299792458 * 60 * 60 * 24">light
                                                                  day</option>
                                                                  <option value="299792458 * 60 * 60 * 24 * 365.25">light
                                                                  year [Julian]</option>
                                                                  <option value="299792458 * 31556925.9747">light
                                                                  year
                                                                  [tropical]</option>
                                                                  <option value="299792458 * 60 * 60 * 24 * 365">light
                                                                  year
                                                                  [traditional]</option>
                                                                  <option value="149597870691 * 206264.8">parsec</option>
                                                                  <option value="1">meter</option>
                                                                  <option value="1609.344">mile</option>
                                                                  </select>
                                                                   </td>
                                                                  <td align="center">To:<br>
                                                                  <select name="to"

                                                                  size="10" onchange="javascript:myCon();">
                                                                  <option selected="selected"

                                                                  value="149597870691">astronomical
                                                                  unit [1996]</option>
                                                                  <option value="1000">kilometer</option>
                                                                  <option value="299792458">light
                                                                  second</option>
                                                                  <option value="299792458 * 60">light
                                                                  minute</option>
                                                                  <option value="299792458 * 60 * 60">light
                                                                  hour</option>
                                                                  <option value="299792458 * 60 * 60 * 24">light
                                                                  day</option>
                                                                  <option value="299792458 * 60 * 60 * 24 * 365.25">light
                                                                  year [Julian]</option>
                                                                  <option value="299792458 * 31556925.9747">light
                                                                  year
                                                                  [tropical]</option>
                                                                  <option value="299792458 * 60 * 60 * 24 * 365">light
                                                                  year
                                                                  [traditional]</option>
                                                                  <option value="149597870691 * 206264.8">parsec</option>
                                                                  <option value="1">meter</option>
                                                                  <option value="1609.344">mile</option>
                                                                  </select>
                                                                  </td>
                                                                  </tr>
                                                                  <tr>
                                                                  <td colspan="2"

                                                                  align="center">
                                                                  <input value="CONVERT!"

name="Go" onclick="javascript:myCon();" type="button"></td>
        </tr>
        <tr>
          <td colspan="2" align="left"><strong>Results: </strong><br>
          <input name="answer" size="70" style="word-spacing: -0.10em" readonly="readonly"

type="text"></td>
        </tr>
      </tbody></table>
      </center></div>
    </form>

<!-- Center Bottom -->
</td></tr></tbody></table></td>



</tr>

</tbody></table></td>
</tr>
</tbody></table>

</center>
</body></html>

这里是我的android代码来加载webview,但我不确定这是否有帮助,因为webview正在加载,只是HTML文件在加载后没有正确使用.js文件。

    WebView lWebView = (WebView)findViewById(R.id.webView1);
    lWebView.loadUrl("file:///android_asset/au.html");

最佳答案

试试这个来启用 JavaScript。

WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

关于javascript - android webview本地html文件与javascript,脚本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46655209/

相关文章:

java - 未找到 JSP 2 标记文件 “/WEB-INF/tags”

android - 如何与信标保持距离

javascript - 客户端网页文本翻译(无需互联网连接)

javascript - 通过javascript获取图片

java - 如何从数组对象中删除一个 £ 符号并保存它?

java - 在 GridView 中滚动时 ImageViews 改变了位置

android - AlarmManager 不适用于时间选择器

javascript - 如何在模式关闭时将选择框设置为默认值?

javascript - Angular 限制对 View 的访问,除非有 cookie

java - X 在 Integer.parseInt(..,X) 中做了什么?