当我尝试从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函数?为什么&#34;功能没有定义&#34;发生错误? 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 >
答案 0 :(得分:-1)
尝试以下步骤:
//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 + "')");
}
});