首页 文章

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

提问于
浏览
0

我正在尝试从webview中的assets文件夹加载本地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");

1 回答

  • 2

    试试这个来启用javascript .

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

相关问题