views:

552

answers:

2

I have a long snippet of HTML I want some javascript variable to equal and for maintainability I want to store it with actual newlines instead of adding \n (or as it is HTML just omitting newline characters). In Python I would do something like:

largeString = """Hello
This is long!"""

And that would work perfectly. However, I haven't seen a way to do this in JavaScript.

Some additional information: the javascript is in an external .js file and the snippet really is quite huge (~6kb).

+1  A: 

Put a \ at the end of each line. Alternatively store it in a div with display:none and then use .html() to retrieve it

brian
Here is exaple of using \ and storing in div http://forums.devx.com/showthread.php?t=154826
mmcteam.com.ua
+2  A: 

JavaScript doesn't support multi-line strings in the same manner.

You can use \ to escape a newline for your script, but this is only for developer reading as it won't stay with the string (and most validators will throw errors for it). To keep a newline with the string, you'll still be required to use \n.

If you want the readability, you can combine them:

var largeString = '"Hello\n\
This is long!"';
Jonathan Lonowski