【问题标题】:android webview local html file with javascript, script not functional带有javascript的android webview本地html文件,脚本不起作用
【发布时间】:2017-10-09 21:25:31
【问题描述】:

我正在尝试从 webview 的 assets 文件夹中加载本地 HTML 文件。我可以让 HTML 文件加载到 webview 中,但是 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>

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

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

【问题讨论】:

  • 您是否在 webview 设置中启用了 Javascript?权限呢?
  • 我该怎么做?我在 webview XML 中没有看到启用 javascript 的选项

标签: javascript java android html


【解决方案1】:

试试这个来启用javascript。

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

【讨论】:

  • 天哪,你是个天才!解决了问题,现在可以正常使用了!
  • 请注意,有人应该在清单文件中包含INTERNET 权限。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-31
  • 2021-12-17
相关资源
最近更新 更多