spanning multiple lines with string text in javascript

notes and quirk solutions found over time
Post Reply
darknkreepy3#
Site Admin
Posts: 247
Joined: Tue Oct 27, 2009 9:33 pm

spanning multiple lines with string text in javascript

Post by darknkreepy3# »

javascript breaks strings of text and considers the next line a line of code to interpret, this allows for single line execution after if statements:

Code: Select all

if(a==b)
     document.write("correct answer!")
instead of a more easily flow read like this (and a semicolon at the end to declare the end of a statement)

Code: Select all

if(a==b)
     {
     document.write("i am easy to read");
     }
beyond that, to get a lot of text to behave as if it were formatted as a long html or php string in javascript, this will not work:

Code: Select all

var str="
<html>
<body>
testing123
</body>
</html>
";
but this will by escaping and marking (\) the newlines to collate or combine in javascript:
*even the first empty line if existant

Code: Select all

var str="\
<html>\
<body>
testing123\
<\body>\
<\html>\
";
you may find this useful for template design and quick html to javascript creation of inline scripting. quick. fast. easy.

but not made for use all of the time and to be lazy.
Post Reply