将来自android资产文件夹的HTML文件中的外部javascript文件加载到WebView中

时间:2018-11-21 14:44:43

标签: javascript android html webview

我正在尝试将外部JavaScript文件加载到HTML文件中。两者都放置在资产文件夹中。

这是我的htmlTest.html文件:

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    <script src="test.js"></script>
</head>

<body>

    <script type="text/javascript">

        func();


    </script>

</body>

这是我的test.js文件:

function func(){
    $(document).ready(function () {
        $('#EDIFICI').children().click(function () {
            alert($(this).attr('id'));
        });

    });
}   

两个文件都位于资产文件夹的根目录中。 我像这样将htmlTest.html加载到我的WebView中:

wv.loadUrl("file:///android_asset/htmlTest.html");  

如果我将JavaScript代码直接内联到HTML页面中,则可以使用,但是当我链接外部JavaScript文件时却无效。

5 个答案:

答案 0 :(得分:0)

尝试类似的方法

webView.evaluateJavascript(yourFileAsString)

答案 1 :(得分:0)

尝试按照以下步骤添加脚本

<html>
  <head>
    <script type="text/javascript" src="test.js"></script>
  </head>

然后将test.js文件添加到带有html的文件夹中

答案 2 :(得分:0)

我的javascript文件出现错误。它的工作方式就像我在示例中所述。抱歉浪费您的时间。

答案 3 :(得分:0)

1。在blob = new Blob([icsFileContents], {type: "application/octet-stream;charset=utf-8"}); var fileSaver = saveAs(blob, "calEvent.ics").onwriteend = function() { setTimeout(function() { window.close(); }, 250); }; 上添加onCreate

2.change .js像这样

getSettings().setJavaScriptEnabled(true)

答案 4 :(得分:0)

这对我有用:

 <head>
        <script type="text/javascript" src="file:///android_asset/xxxxx.js"></script>
</head>

xxxxxx.js位于资源文件夹中。

在代码中

WebView.enableSlowWholeDocumentDraw();
        oWebView.clearCache(true);
        oWebView.setPadding(0, 0, 0, 0);
        oWebView.setInitialScale(1);
        oWebView.getSettings().setUseWideViewPort(true);
        oWebView.getSettings().setLoadWithOverviewMode(true);
        oWebView.getSettings().setSupportZoom(true);
        oWebView.getSettings().setBuiltInZoomControls(true);
        oWebView.setScrollbarFadingEnabled(false);
        oWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        oWebView.getSettings().setJavaScriptEnabled(true);
        oWebView.setWebViewClient(new WebViewClient());