Newline character in Javascript

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
littlecoder
Forum Commoner
Posts: 26
Joined: Fri Oct 17, 2008 4:36 am

Newline character in Javascript

Post by littlecoder »

Hi ,
I am unable to use the newline character in javascript . My code is :
<html>
<head>
</head>
<body>
<script type="text/javascript">
document.write("Hello world \n Welcome to Javascript");
</script>
</body>
</html>
The output is : Hello world Welcome to Javascript.
Can anyone help ?
sirish
Forum Newbie
Posts: 8
Joined: Tue Sep 09, 2008 11:45 pm

Re: Newline character in Javascript

Post by sirish »

Try the following
<html>
<head>
</head>
<body>
<script type="text/javascript">
document.write("Hello world <br> Welcome to Javascript");
</script>
</body>
</html>
littlecoder
Forum Commoner
Posts: 26
Joined: Fri Oct 17, 2008 4:36 am

Re: Newline character in Javascript

Post by littlecoder »

Hi ,

Thanks a lot.
It worked!!
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Newline character in Javascript

Post by Jonah Bron »

The problem is not javascript. The problem is HTML. HTML does not recognize new lines. It only recognizes only line breaks (<br />, or <br>). Javascript is giving the new line, and it is there, but it isn't visualization is not initiated by the HTML engine.

Note: using the <pre></pre> tags enable you to use line breaks inside, along with extra spaces.
Post Reply