/    Sign up×
Community /Pin to ProfileBookmark

n new line character won’t work

Hello,
I am studying javascript from eloquent javascript book.
It says:
“This is the first linenAnd this is the second”
I tried making a web page by writing
document.write(“This is the first linenAnd this is the second”);

The desired effect is:

This is the first line
And this is the second

But I get this:

This is the first line And this is the second

I tried writing <br> inside and it worked. But Why writing an escape n didn’t? Anything I am doing wrong? I am using firefox

Thank you

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERDec 27.2012 — You are doing nothing wrong. The n character is valid in text related elements like, alert() and <textarea>.

The document.write() command writes to the body of the site or the display. Substitute <br> in place of the n here.
Copy linkTweet thisAlerts:
@smirnoffauthorDec 28.2012 — Ah thank you, the book did not clarify that.
Copy linkTweet thisAlerts:
@ReFreezedDec 28.2012 — To add to what JMRKER said, whitespace is handled automatically if it's in the body of your page, but not when used in alert boxes and input elements etc. So all your whitespace characters (spaces, new lines etc.) are treated like a single space until the text needs to wrap to the next line, in which case the whitespace character (whatever it is) is treated like a newline character.

Another example that would show this would be writing several spaces after each other:
alert("Hello foo bar."); // shows several spaces between foo and bar
document.write("Hello foo bar."); // shows only a single space
Copy linkTweet thisAlerts:
@JMRKERDec 28.2012 — Note also that <p> can be used to replace <br><br>

and &nbsp; can be use in the body to force extra spaces to be visible,

as in &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; to show 5 blank spaces.

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; could be used to display 9 spaces between visible text characters
Copy linkTweet thisAlerts:
@ReFreezedDec 28.2012 — &nbsp; can be use in the body to force extra spaces to be visible,

as in &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; to show 5 blank spaces.[/QUOTE]


Although, the CSS properties 'margin' and 'padding' should be used to add extra space around and inside an element if possible.

(It feels like we're slowly drifting farther and farther away from the topic by every post. lol)
×

Success!

Help @smirnoff spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...