Android webview"Uncaught SyntaxError:Unexpected token ILLEGAL"

Aru*_*ose 8 javascript android webview

在我的android程序中,我得到了一个webview,需要动态地为webview元素(textareas,复选框等)设置值.我有一个javascript方法,它从程序接收值并执行字符串操作并将值存储到正确的元素.但我总是得到这个错误...有点卡在这里.任何帮助将不胜感激.

我成功地在w3schools TryIt编辑器中执行了该脚本,但没有在该程序中工作!

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);
Run Code Online (Sandbox Code Playgroud)

我的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];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

值在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+"));");
                }
            }
Run Code Online (Sandbox Code Playgroud)

在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>
Run Code Online (Sandbox Code Playgroud)

传递给onPageFinished的字符串:

69_206-@!@-MyName||
Run Code Online (Sandbox Code Playgroud)

我在运行我的应用程序时得到的错误是

11-23 14:46:59.786: I/chromium(2763): [INFO:CONSOLE(1)] "Uncaught SyntaxError: Unexpected token ILLEGAL", source:  (1)
Run Code Online (Sandbox Code Playgroud)

我尝试在w3schools Tryit Editor中运行脚本并且脚本成功执行,但从未在android应用程序中运行..我的错误是什么?如果你可以帮忙的话会很棒!!

提前致谢..

Aru*_*ose 13

发现错误.在作为参数传递时,您需要将String值放在引号中.

错误

view.loadUrl("javascript:window.HtmlViewer.injectIntoWebView(myFunction("+answerString+"));");
Run Code Online (Sandbox Code Playgroud)

正确

view.loadUrl("javascript:window.HtmlViewer.injectIntoWebView(myFunction('"+answerString+"'));");
Run Code Online (Sandbox Code Playgroud)

值应该在引号中,检查 myFunction('69_206-@!@-MyName||')