Android web browsing "Uncaught SyntaxError: Unexpected ILLEGAL token"

In my Android program, I got a web view and must dynamically set the values ​​of the webview elements (text fields, checkboxes, etc.). I have a javascript method that gets values ​​from a program and performs string operations and saves the values ​​to the correct element. But I always get this error ... stuck here. Any help would be appreciated.

I successfully executed the script in the Try3t editor for w3schools, but did not work 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); 

And my Javascript method for setting values ​​for text areas in 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]; } } } 

Values ​​are passed by 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+"));"); } } 

HTML content loaded in webView, questionArray [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 onPageFinished:

 69_206-@!@-MyName|| 

The error I get when starting 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 w3schools Tryit Editor, and the script executed successfully, but never in an Android app. What is my mistake? It would be great if you could help!

Thanks in advance.

+8
javascript android webview
source share
2 answers

An error has been detected. You must put the String value in quotation marks, passing it as a parameter.

Wrong

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

Correctly

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

The value must be in quotation marks, check myFunction('69_206-@!@-MyName||')

+12
source share

Check your JavaScript file

if you have the wrong syntax for the relational operator in an if statement.

change => 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(); } });​ 

Others check this code:

If you need to make sure that a string is not just an empty void (I assume it is for form validation) you need to make a replacement in spaces.

 str.replace(/-!!!-/g,"") 
+1
source share

All Articles