wood burning stoves 2.0*
The moose likes JSP and the fly likes Setting JSP with newline character to Javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Setting JSP with newline character to Javascript" Watch "Setting JSP with newline character to Javascript" New topic
Author

Setting JSP with newline character to Javascript

venkat warlu
Greenhorn

Joined: Mar 05, 2008
Posts: 7
Hi all, Thanks in advance. I am trying to set a text area string with newline characters in it to a java script variable using jsp like below:

document.form.textarea.value = '<%=jspStringVariable%>';

But it is throwing error like "Unterminated String Constant" error. How to set the jsp value to my java script.

Actually i am trying to retain my value in textarea while i self submit the form to reload some other option values.

Regards
Venkat

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You should modify that string by replacing each of the newline characters by the string "\n", which is the Javascript escape for the newline.
venkat warlu
Greenhorn

Joined: Mar 05, 2008
Posts: 7
Paul, I am trying to do something like this:

<% String str ="";
for(int i=0; i < TempStr.length(); i++){
if(Character.getNumericValue(TempStr.charAt(i))<0){
str = str + "\n";
}else{
str = str + TempStr.charAt(i);
}
}
%>

And then in JS:
<script>
function fn(){
document.form.textarea.value = '<%=str%>';
}
</script>

It is still not working.

Regards
Venkat

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65

Have you looked at the HTML and script that's being sent to the client? Is it correct? if not, what can you do to correct it?

Just saying "it doesn't work" isn't helping anyone help you.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
venkat warlu
Greenhorn

Joined: Mar 05, 2008
Posts: 7
Thanks Bear for the code tag link. I worked it out myself. I replaced character by character as suggested by Paul. Thanks for your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Setting JSP with newline character to Javascript
 
Similar Threads
Problem with script tag
invoke a java function onSubmit of the form
OpenCSV Error on server
Getting Syntax Error in Task Bar
Problem with closing script tag