繁体   English   中英

从WebView调用JS函数会产生错误“函数未定义”

[英]Calling the JS function from WebView produces an error “function is not defined”

当我尝试从WebView调用JS函数MyApp_HighlightAllOccurencesOfString()时

String textToSearch = editTextSearchInText.getText().toString();
webSettings.setJavaScriptEnabled(true);
article_web_view.evaluateJavascript("MyApp_HighlightAllOccurencesOfString('" + textToSearch + "')", new ValueCallback<String>() {
    @Override
    public void onReceiveValue(String s) {
        Toast.makeText(getBaseContext(), s, Toast.LENGTH_LONG).show();
    }
});

返回null,控制台如下所示:

"Uncaught ReferenceError: MyApp_HighlightAllOccurencesOfString is not defined", source:  (1)

请告诉我,如何正确地从WebView调用JS函数? 为什么会出现“未定义功能”错误? 来自WebView的JS代码如下:

<script > 
var MyApp_SearchResultCount = 0;
function MyApp_HighlightAllOccurencesOfStringForElement (element, keyword){
    if (element) {
        if (element.nodeType == 3) {
            while (true) {
                var value = element.nodeValue;
                var idx = value.toLowerCase().indexOf(keyword);
                if (idx < 0) break;
                var span = document.createElement("span");
                var text = document.createTextNode(value.substr(idx, keyword.length));
                span.appendChild(text);
                span.setAttribute("class", "MyAppHighlight");
                span.style.backgroundColor = "#C5DFEA";
                text = document.createTextNode(value.substr(idx + keyword.length));
                element.deleteData(idx, value.length - idx);
                var next = element.nextSibling;
                element.parentNode.insertBefore(span, next);
                element.parentNode.insertBefore(text, next);
                element = text;
                window.scrollTo(0, findPos(span));
                MyApp_SearchResultCount++;
            }
        } else if (element.nodeType == 1) {
            if (element.style.display != "none" && element.nodeName.toLowerCase() != "select") {
                for (var i = element.childNodes.length - 1; i >= 0; i--) {
                    MyApp_HighlightAllOccurencesOfStringForElement(element.childNodes[i], keyword);
                }
            }
        }
    }
}
function findPos (obj) {var curtop = -100; if (obj.offsetParent) {
    do {
        curtop += obj.offsetTop;
    } while (obj = obj.offsetParent);
    return[curtop];
}}
function MyApp_HighlightAllOccurencesOfString (keyword) {MyApp_RemoveAllHighlights();
MyApp_HighlightAllOccurencesOfStringForElement(document.body, keyword.toLowerCase());}function MyApp_RemoveAllHighlightsForElement (element) {
if (element) {
    if (element.nodeType == 1) {
        if (element.getAttribute("class") == "MyAppHighlight") {
            var text = element.removeChild(element.firstChild);
            element.parentNode.insertBefore(text, element);
            element.parentNode.removeChild(element);
            return true;
        } else {
            var normalize = false;
            for (var i = element.childNodes.length - 1; i >= 0; i--) {
                if (MyApp_RemoveAllHighlightsForElement(element.childNodes[i])) {
                    normalize = true;
                }
            }
            if (normalize) {
                element.normalize();
            }
        }
    }
}
return false;}
function MyApp_RemoveAllHighlights () {
    MyApp_SearchResultCount = 0;
    MyApp_RemoveAllHighlightsForElement(document.body);
}
function findImage (x, y){
    return document.elementFromPoint(x, y).src;
}</script >

尝试以下步骤:

//set webviewclient
    article_web_view.setWebViewClient(new WebViewClient());
//enable javascript
    article_web_view.getSettings().setJavaScriptEnabled(true);
//webchromeclient
    article_web_view.setWebChromeClient(new WebChromeClient());

用函数js加载文件

    article_web_view.loadUrl("file:///android_asset/myfunctions.html");

然后初始化并运行您的js脚本

    article_web_view.setWebViewClient(new WebViewClient() {
                    public void onPageFinished(WebView view, String url) {
                        //prefix javascript
wbChart.loadUrl("javascript:MyApp_HighlightAllOccurencesOfString('" + textToSearch + "')");

                    }
                });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM