Android WebView:使用脚本标记从我的资产文件夹加载外部javascript文件

Android WebView:使用脚本标记从我的资产文件夹加载外部javascript文件,javascript,android,html,android-webview,external-js,Javascript,Android,Html,Android Webview,External Js,我有一个android项目,它有一个网络视图。此webview加载存储在“我的资产”文件夹中的静态html文件。我的资产文件夹中还有一个第三方javascript库。在我的html文件头部的脚本标记中有一些javascript。javascript引用rangy库中的方法和对象。我的html文件通过一个脚本标记引用rangy。当我尝试使用函数和方法时,会出现如下错误: Cannot call method '[any method from rangy library]' of

我有一个android项目,它有一个网络视图。此webview加载存储在“我的资产”文件夹中的静态html文件。我的资产文件夹中还有一个第三方javascript库。在我的html文件头部的脚本标记中有一些javascript。javascript引用rangy库中的方法和对象。我的html文件通过一个脚本标记引用rangy。当我尝试使用函数和方法时,会出现如下错误:

    Cannot call method '[any method from rangy library]' of 
    undefined
下面是相关的代码片段

这就是我包括兰吉的地方:

<html lang="en">
<head>
    <title> </title>
    <meta charset="utf-8">
    <style type="text/css">
    </style>
    <script type="text/javascript" src="rangy.js"></script>
    <script type="text/javascript" src="rangy-serializer.js"></script>
    <script type="text/javascript">

    //...My native js

    </script>
</head>
下面是我的原生js中引用rangy的部分。末尾的catch块将错误打印到html文档的正文中,因为android webview默认不提供此信息:

jsHandler.restoreSelection = function(selectionDetails) {

        try{
            window.document.body.style.background="yellow";
            window.rangy.deserializeSelection(decodeURIComponent(selectionDetails.replace(/\s+$/, "")));
            window.document.body.style.background="green";
        }
        catch(err){
            window.document.body.innerHTML = err.message;
        } 
    };
我的应用程序中有一个按钮,可以启动jsHandler.restoreSelection函数。当我按下该按钮时,背景从白色变为黄色,但随后该命令捕捉到一个异常,并将正文文本替换为我在顶部发布的错误消息。我的应用程序设置有什么问题?从“我的资产”文件夹中的html文件引用外部js文件的正确方法是什么?注意:我确实打开了javascript


提前感谢

在又一天的谷歌搜索之后,我发现了这个页面,这是我问题的解决方案:我使用loadDataWithBaseUrl将数据传递到webview,但我将baseurl作为null传递,因为我不知道如何处理它。相反,我将传递一个对我的项目的资产目录的引用

在又一天的谷歌搜索之后,我发现了这个页面,这是我问题的解决方案:我使用loadDataWithBaseUrl将数据传递到webview,但我将baseurl作为null传递,因为我不知道如何处理它。相反,我将传递一个对我的项目的资产目录的引用

jsHandler.restoreSelection = function(selectionDetails) {

        try{
            window.document.body.style.background="yellow";
            window.rangy.deserializeSelection(decodeURIComponent(selectionDetails.replace(/\s+$/, "")));
            window.document.body.style.background="green";
        }
        catch(err){
            window.document.body.innerHTML = err.message;
        } 
    };