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.
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);
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];
}
}
}
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+"));");
}
}
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>
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)
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!!
Thanks in advance..
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.
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);
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];
}
}
}
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+"));");
}
}
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>
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)
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!!
Thanks in advance..
Share Improve this question edited Nov 23, 2013 at 9:25 Arun Jose asked Nov 23, 2013 at 7:36 Arun JoseArun Jose 1,8571 gold badge15 silver badges24 bronze badges 15- what should be need to replace string by javascript. – codercat Commented Nov 23, 2013 at 7:56
- str.replace("/-!!!-/","") check this – codercat Commented Nov 23, 2013 at 8:03
- what you want to replace empty either null – codercat Commented Nov 23, 2013 at 8:23
- 1 check your split method – codercat Commented Nov 23, 2013 at 8:58
- 1 Hi, please use this for replace var_.replace(/-!!!-/g,"") – codercat Commented Nov 23, 2013 at 9:10
2 Answers
Reset to default 16Found 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||')
Check your JavaScript file
if you have Wrong syntax of relational operator in 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();
}
});
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,"")