javascript Android webview“未捕获的语法错误:意外的令牌非法”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20159855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android webview "Uncaught SyntaxError: Unexpected token ILLEGAL"
提问by Arun Jose
In my android program I got a webview and need to set values to the webview elements (textareas, checkboxes etc) dynamically. i have a javascript method which receives values from the program and perform string operations and stores the values to the correct element. But i get this error always... kinda stuck here. Any help will be appreciated.
在我的 android 程序中,我有一个 webview,需要动态地为 webview 元素(文本区域、复选框等)设置值。我有一个 javascript 方法,它从程序接收值并执行字符串操作并将值存储到正确的元素。但我总是收到这个错误......有点卡在这里。任何帮助将不胜感激。
I successfully executed the script in the w3schools TryIt Editor, but not working in the program!
我在 w3schools TryIt Editor 中成功执行了脚本,但在程序中不起作用!
final WebView webView = new WebView(getApplicationContext());
LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1);
webView.setLayoutParams(params);
webView.setBackgroundColor(Color.LTGRAY);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.setWebChromeClient(new android.webkit.WebChromeClient());
webView.setWebViewClient(new WebChromeClient());
webView.loadData("<!DOCTYPE html><html><body>"+ questionsArray[questionIndex] +"</body></html>", "text/html", "UTF-8");
webView.addJavascriptInterface(javaScriptInterface, "HtmlViewer");
scrolRootLayout.addView(surveyWebView);
And my Javascript method to set values for the textareas in the webView
和我的 Javascript 方法来为 webView 中的 textareas 设置值
function myFunction(var_)
{
var str = var_.replace("-!!!-","");
var pipeSplitedArray = str.split("||");
for(var i=0; i<pipeSplitedArray.length-1; i++) {
if(pipeSplitedArray[i].indexOf("-!@!-") == -1) {
var queArray = new Array();
queArray =pipeSplitedArray[i].split("-@!@-");
if(document.getElementsByName(queArray[0]))
document.getElementsByName(queArray[0]).item(0).innerHTML=queArray[1];
}
}
}
Values are passed on onPageFinished loading..
值在 onPageFinished 加载时传递..
private class WebChromeClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
String script = "javascript:function myFunction(var_) { var pipeSplitedArray = var_.split(\"\|\|\"); for(var i=0; i<pipeSplitedArray.length-1; i++) { if(pipeSplitedArray[i].indexOf(\"-!@!-\") == -1) { var queArray = new Array(); queArray =pipeSplitedArray[i].split(\"-@!@-\"); if(document.getElementsByName(queArray[0])) document.getElementsByName(queArray[0]).item(0).innerHTML=queArray[1]; } } }";
view.loadUrl(script);
view.loadUrl("javascript:window.HtmlViewer.injectIntoWebView(myFunction("+answerString+"));");
}
}
The HTML content loaded in the webView, questionsArray[questionIndex] is
webView加载的HTML内容, questionsArray[questionIndex] 是
<div class="newmain5 norma12" style="position:relative;width:320px;"> Enter your name :</div>
<div class="newmain6" style="position:relative;width:275px;left:10px;">
<textarea name="69_206" id="1" rows="5" cols="30" style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#666666; width:260px; border:1px solid #CCCCCC; background-color:#EBEBEB;" />
</textarea>
</div>
String passed on onPageFinished:
传递给 onPageFinished 的字符串:
69_206-@!@-MyName||
The Error i get all time while running my application is
我在运行我的应用程序时一直遇到的错误是
11-23 14:46:59.786: I/chromium(2763): [INFO:CONSOLE(1)] "Uncaught SyntaxError: Unexpected token ILLEGAL", source: (1)
I tried running the script in the w3schools Tryit Editor and the script is successfully executed, but never in the android application.. whats my mistake? Would be great if you can help!!
我尝试在 w3schools Tryit Editor 中运行脚本并且脚本成功执行,但从未在 android 应用程序中运行..我的错误是什么?如果你能帮忙就太好了!!
Thanks in advance..
提前致谢..
回答by Arun Jose
Found the Error. You need to put the String value in quotes while passing as parameter.
发现错误。作为参数传递时,您需要将字符串值放在引号中。
Wrong
错误的
view.loadUrl("javascript:window.HtmlViewer.injectIntoWebView(myFunction("+answerString+"));");
Correct
正确的
view.loadUrl("javascript:window.HtmlViewer.injectIntoWebView(myFunction('"+answerString+"'));");
The value should be in quotes, check myFunction('69_206-@!@-MyName||')
值应该用引号引起来,检查 myFunction('69_206-@!@-MyName||')
回答by codercat
Check your JavaScript file
检查您的 JavaScript 文件
if you have Wrong syntax of relational operator in if statement.
如果在 if 语句中有错误的关系运算符语法。
change => to >= and =< to <=
改变 => to >= and =< to <=
$('#search').bind('keydown', function() {
tmp = Number(event.keyCode);
if ((tmp <= "48" && tmp >= "90") || (tmp <= "96" && tmp >= "111") || (tmp <= "186" && tmp >= "222")) {
showSearching();
}
});?
Other check this your code:
其他检查这个你的代码:
If you need to make sure that the string is not just a bunch of empty spaces (I'm assuming this is for form validation) you need to do a replace on the spaces.
如果您需要确保字符串不仅仅是一堆空格(我假设这是用于表单验证),您需要对空格进行替换。
str.replace(/-!!!-/g,"")