How to write Multiple Lines in JS?

W

Wow

Do I have to replace all new lines with \n and document.write one line? Is
there something like perl's print lines

print << 'body';



body

in javascripts?
 
G

Grant Wagner

Wow said:
Do I have to replace all new lines with \n and document.write one line? Is
there something like perl's print lines

print << 'body';

body

in javascripts?

No, JavaScript has no concept like the here document in Perl. The best you can
do is use something like the following if you have lots of stuff to
document.write():

document.write(
'stuff' +
' more stuff' +
' even more stuff' +
);

Since including \n in the output of a document.write() into the <body> of an
HTML document simply results in a space in the output, you might as well use
spaces and save yourself some typing. Another way to do this would be:

var output = [
'stuff',
'more stuff',
'even more stuff'
];
document.write(output.join('\n'));
 
K

kaeli

Do I have to replace all new lines with \n and document.write one line? Is
there something like perl's print lines

print << 'body';

IF I understand your question correctly...

var myString;
myString = "This is a line of text " +
"that I want over multiple lines. " +
"That way, I don't have to make one big line of "+
"unreadable text.";

document.write(myString);

The string will be all on one line in the browser.

As to a construct like the HERE doc in Perl, no, no such animal in JS AFAIK.

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top