Android webview" Uncaught SyntaxError:Unexpected token ILLEGAL" [英] Android webview "Uncaught SyntaxError: Unexpected token ILLEGAL"

查看:840
本文介绍了Android webview" Uncaught SyntaxError:Unexpected token ILLEGAL"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我在w3schools TryIt编辑器中成功执行了该脚本,但未在该程序中运行!

I successfully executed the script in the w3schools TryIt Editor, but not working in the program!

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);

我的Javascript方法为webView中的textareas设置值

And my Javascript method to set values for the textareas in the webView

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];
        }
    }
}

值传递onPageFinished loading ..

Values are passed on onPageFinished loading..

    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+"));");
                }
            }

webView中加载的HTML内容,questionsArray [questionIndex ]是

The HTML content loaded in the webView, questionsArray[questionIndex] is

<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>

在onPageFinished上传递的字符串:

String passed on 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)

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

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!!

提前致谢..

推荐答案

发现错误。您需要在传递参数时将字符串值放在引号中。

Found the Error. You need to put the String value in quotes while passing as parameter.

错误

view.loadUrl("javascript:window.HtmlViewer.injectIntoWebView(myFunction("+answerString+"));");

正确

view.loadUrl("javascript:window.HtmlViewer.injectIntoWebView(myFunction('"+answerString+"'));");

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

The value should be in quotes, check myFunction('69_206-@!@-MyName||')

这篇关于Android webview&quot; Uncaught SyntaxError:Unexpected token ILLEGAL&quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆