[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2802
  • Last Modified:

Javascript: How to add multiline text in variable without all the quote


   I was wondering if there was a alternative way than doing something like this

TextVar = "This is the first line\r\n"
              +"This is the second line\r\n"
              +"This is the third line\r\n"

I would like something like this instead or maybe another way to put text in a simple way so I don't have to edit too much.
I could put them in a file and load them but the number of file would be huge and it would be really slow to load all this of a server.
I don't know if XML would be a good thing and how to use it but that is another storie, would be nice to know what best solution is fit for
this. I run this from HTA, I'm designing a template selector so I don't have to text or search text to put in document all the time.
I guess it's all about not wasting time ;)

TextVar = "
This is the first line
This is the second line
This is the third line

  • 2
  • 2
1 Solution
Nope, the code you are currently using is the only way to go.
DogofwarsAuthor Commented:
Can't even use a alternative, like XML or somthing simple??
You can use \ to make multiline strings (still need to use \r\n though).  JS sees \ separated lines as one big line.  Or, you can put all the strings in an array and join them on newlines (method 2).

Can you generate the strings on the server side and print them that way?

//method one
var myString = "\
Hello, \r\n\
There is another... \r\n\
Sky \r\n\


//method two
var myArray = new Array();
myArray.push("There is another...");

myString = myArray.join("\r\n");


saddy1 said it all... it should not be hard to write server-side code that formats the text correctly (a simple replace should do the job).  also, you don't need to define each text in a separate file, just define the different text values in different variables in one single file (that's how localization is usually done).
DogofwarsAuthor Commented:
I wonder if XML would do the trick???

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now