PDA

View Full Version : Populating a variable using ASP



Buddy Bradley
June 21st, 2001, 12:01
I am trying to populate a javascript variable with a string from a database using ASP. The string stretches over a number of lines:
this is line one
this is line two

This means that when the asp writes the string into the variable, it writes:
var my_variable = "this is line one
this is line two
;"

This is causing an "unterminated string constant error" (cos the javascript line spills onto three lines in the above example). Can anyone tell me how to get round this?

ashben
June 21st, 2001, 13:44
Try:

<script language="JavaScript">
:
:
var my_var = "<%=server.htmlencode(StringVar)%>";
:
:
</script>

puDDs
June 21st, 2001, 21:22
Originally posted by Buddy Bradley
I am trying to populate a javascript variable with a string from a database using ASP. The string stretches over a number of lines:
this is line one
this is line two

This means that when the asp writes the string into the variable, it writes:
var my_variable = "this is line one
this is line two
;"

This is causing an "unterminated string constant error" (cos the javascript line spills onto three lines in the above example). Can anyone tell me how to get round this?

Try using the linebreak escape character (\n) instead of a hard return...like this:

var my_variable = "this is line one\nthis is line two";