0

I want all the stings of this code on separate lines, but it all goes on one line. I have already tries \r and \n here are the first few lines:

 document.write("you are an explorer, entering a dragons cavern in hopes of treasure");
 document.write("be warned, the caverns are always changing");
 document.write("...");
3
  • Have you tried <br />? (If the document is XHTML.) Commented Jan 2, 2014 at 17:58
  • If you are outputting HTML, you need <br> Commented Jan 2, 2014 at 17:59
  • By the way - is this going to be a remake of the classic? Commented Jan 2, 2014 at 18:00

5 Answers 5

10

\r and \n are for line breaks in the document, which doesn't matter as far as what is rendered (html) unless you're within a <pre> tag or using the css property whitespace to have the document whitespace rendered. In html a line break is <br>.

While the <br> tag is sometimes useful, don't use it without reason. For example, if you want to split up two paragraghs, you would have markup like this:

<p>This is paragraph 1.</p>
<p>This is paragraph 2.</p>

To make a space in between them, you could use css style:

p {
  display: block;
  margin-botton: 20px;
}

In this case, you should NOT use the <br> tag like this:

<p>This is paragraph 1.</p>
<br>
<p>This is paragraph 2.</p>

Also note that document.write is a dangerous practice and is almost never necessary. Rather than using document.write, you can use javascript to create/manipulate elements like this:

var p = document.createElement('p');
p.textContent = 'This is paragraph 1.';
document.body.appendChild(p);
Sign up to request clarification or add additional context in comments.

Comments

3

document.write produces HTML. Whitespace is condensed in HTML, so if you need a linebreak, you'll need to use a <br> element.

document.write("you are an explorer, entering a dragons cavern in hopes of treasure<br>");
document.write("be warned, the caverns are always changing<br>");
document.write("...<br>");

Also, you don't need to break the document.write string into multiple calls:

document.write('you are an explorer, entering a dragons cavern in hopes of treasure<br>be warned, the caverns are always changing<br>...');

Generally speaking, document.write should be discouraged because it will rewrite the entire page if called after the document is closed to writing. Typically these sorts of changes are done through DOM nodes:

document.body.innerHTML = 'you are an explorer, entering a dragons cavern in hopes of treasure<br>be warned, the caverns are always changing<br>...';

Alternatively, if you need to guarantee formatting of text, you could use a <pre> element (or CSS with white-space: pre). Preformatted text allows newlines and multiple spaces to be respected:

<pre>you are an explorer, entering a dragons cavern in hopes of treasure
be warned, the caverns are always changing
...</pre>

Comments

0

If you must do it using document, use document.writeln instead of document.write

Comments

0

You'll need to use HTML tags:

document.write("<p>you are an explorer, entering a dragons cavern in hopes of treasure</p>");
document.write("<p>be warned, the caverns are always changing</p>");
document.write("<p>...</p>");

1 Comment

Please do not encourage the use of document.write in inappropriate cases like this. Also, the html tags themselves don't determine spacing. That would be up to the css styles applied to them.
0

when using document.write(); you need to realize that this is HTML that it is producing, therefore, you should use <br> to create the desired result. Besides, this method is not really highly favored.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.