Page 1 of 1

Newline character in Javascript

Posted: Tue Nov 04, 2008 9:49 pm
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 ?

Re: Newline character in Javascript

Posted: Tue Nov 04, 2008 9:52 pm
by sirish
Try the following
<html>
<head>
</head>
<body>
<script type="text/javascript">
document.write("Hello world <br> Welcome to Javascript");
</script>
</body>
</html>

Re: Newline character in Javascript

Posted: Tue Nov 04, 2008 10:53 pm
by littlecoder
Hi ,

Thanks a lot.
It worked!!

Re: Newline character in Javascript

Posted: Sun Dec 07, 2008 3:03 pm
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.