<script type='text/javascript'>
//<![CDATA[
for(var j = 0; j < 6;) {
document.writeln(++j);
}
//]]>
</script>
in the above code it suppose to give a new line if referring to the definition of the The writeln() method is identical to the write() method, with the addition of writing a newline character after each statement.
But in my case the output is 123456
UPDATE:
I will find the best answer to accept but with all the answers are correct and I believe next time someone will have the same question, so I thought I should just update with the answer to the top and remove the homework tag so this will benefit everyone.
The reason that is not producing the new line is that because it is actually producing the '\n'
The '\n' to the browser if I'm not wrong it doesn't affect how to display to the screen but if you look at the source-code of the page you will see that the output of mine would display something like this
1
2
3
4
5
6
But if we want it to display it to the page we would have to add the <br\>
tag so it will produce a break-line
so we will get the same output that we want.
<script type='text/javascript'>
//<![CDATA[
for(var j = 0; j < 6;) {
document.writeln(++j);
document.writeln("<br>");
}
//]]>
</script>
<script type='text/javascript'>
//<![CDATA[
for(var j = 0; j < 6;) {
document.writeln(++j);
}
//]]>
</script>
in the above code it suppose to give a new line if referring to the definition of the The writeln() method is identical to the write() method, with the addition of writing a newline character after each statement.
But in my case the output is 123456
UPDATE:
I will find the best answer to accept but with all the answers are correct and I believe next time someone will have the same question, so I thought I should just update with the answer to the top and remove the homework tag so this will benefit everyone.
The reason that is not producing the new line is that because it is actually producing the '\n'
The '\n' to the browser if I'm not wrong it doesn't affect how to display to the screen but if you look at the source-code of the page you will see that the output of mine would display something like this
1
2
3
4
5
6
But if we want it to display it to the page we would have to add the <br\>
tag so it will produce a break-line
so we will get the same output that we want.
<script type='text/javascript'>
//<![CDATA[
for(var j = 0; j < 6;) {
document.writeln(++j);
document.writeln("<br>");
}
//]]>
</script>
Share
Improve this question
edited Apr 20, 2012 at 13:40
Ali
asked Apr 20, 2012 at 13:28
AliAli
10.5k21 gold badges74 silver badges108 bronze badges
3
- 1 I was trying not to give the answer directly.. as it is listed as homework! But nvm! – Arth Commented Apr 20, 2012 at 13:33
- 1 @Arth It may be homework for Ali, but these answers are for more than just one person :) – Sampson Commented Apr 20, 2012 at 13:34
- 1 @JonathanSampson then they can re-ask it without the homework tag! – Arth Commented Apr 20, 2012 at 13:35
7 Answers
Reset to default 6In most cases, the browser does not display '\n'
as a line break.
This can be changed with CSS or <pre>
tags; otherwise, use <br/>
(not great) or block-level elements (<div>
, <p>
, etc.) to introduce breaks in the text.
Think about how the following HTML
<p>
1
2
3
4
</p>
would be displayed in a browser.
No, it is working correctly. The issue here is that you're outputting it on different lines but it will not be displayed that way in the browser.
To view your actual result use "View Source" or look at the HTML in the developer tools in your browser.
If you want this to display on different lines throw in some HTML like the following.
<script type='text/javascript'>
//<![CDATA[
for(var j = 0; j < 6;) {
document.write(++j);
document.writeln("<br>");
}
//]]>
</script>
Or use a PRE
(aka preformatted) tag (see W3 spec info on PRE here), which will preserve line breaks in the underlying source.
The \n
is only visible in code source or within a <pre></pre>
tag. If you are displaying the output within an HTML document, you have to add <br />
to get a new line.
It is writing to new lines, only the browser won't show you those lines unless you specifically instruct it to do so. Look at the effect of adding preformatting tags around the output:
var i = 0;
document.write('<pre>');
while ( i < 5 ) document.writeln(++i);
document.write('</pre>');
The best way to verify this type of output is by viewing the source of the page, and not the page itself.
If you snoop the source with Firebug and copy it into somelike like Noteapd++ you'll find that it does indeed output newline characters. They don't display in the browser because newlines in HTML are not defined as linefeeds.
You need to use <br/>
for that. Go through W3Schools tutorial here.