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

Posted on 2005-05-09
Last Modified: 2008-03-17

   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

Question by:Dogofwars
    LVL 30

    Expert Comment

    Nope, the code you are currently using is the only way to go.

    Author Comment

    Can't even use a alternative, like XML or somthing simple??
    LVL 1

    Accepted Solution

    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");


    LVL 30

    Expert Comment

    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).

    Author Comment

    I wonder if XML would do the trick???

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: Create Mobile App Prototypes with Adobe XD

    This is a project-based course: we go through all the steps of creating a prototype from start to finish, using all the tools and features currently available in Adobe XD. You can complete the course in less than a day, plus all project files and fonts are included.

    Suggested Solutions

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    Building “do-it-yourself” web sites has become an epidemic. There are so many blogs, web sites and even books that "teach" you how to build your web site in a few extremely simple and easy steps. Building a web site has become easier than boiling an…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now