Question : Inserting Line Break in JSP String being displayed by Javascript

Hello,

I'm creating a string in the Java section of a JSP, reading that value in a Javascript section, and trying to display it in an alert box.  However, the line breaks are not working.

I've attached the code I'm trying now.  I'm also tried replacing \n with <br> to no avail.
1:
2:
3:
4:
5:
6:
javaString = "First Line of text:" + "\n" + variable1 + " for " + variable2 + "\n";

<script language="JavaScript" >
	var message="<%=javaString %>";
	alert(message);
</script>

Answer : Inserting Line Break in JSP String being displayed by Javascript

double escape you line break, so the code in javascript becomes the "\n"
1:
2:
3:
4:
5:
6:
7:
8:
<% 
javaString = "First Line of text:" + "\\n" + variable1 + " for " + variable2 + "\n";
%>

<script language="JavaScript" >
	var message="<%=javaString %>";
	alert(message);
</script>
Random Solutions  
 
programming4us programming4us