Solved

document.write vs document.writeln

Posted on 2011-02-14
5
598 Views
Last Modified: 2012-05-11
Hi
I can't see any difference between document.write and document.writeln. I searched the internet and found the followng example if I use the <pre> it works fine if I remove the <pre> it does not work. so what is this <pre>
Why write and writeln does not work without it?
What pre is used for?
Thanks

<html>
<body>

<pre>
<script type="text/javascript">
document.write("line1");
document.writeln("line2");
document.write("line3");
</script>
</pre>
</body>
</html>
0
Comment
Question by:jean11
  • 2
  • 2
5 Comments
 
LVL 16

Accepted Solution

by:
sjklein42 earned 63 total points
Comment Utility
writeln adds a "newline" character to the end of its output.  write does not.

<pre> causes the output to be shown "literally".  It is meant for what they call "preformatted" text and usually is displayed in a fixed font like Courier rather than a proportional font.

Within a <pre> block, newline characters are preserved.  Outside a <pre> block, newlines are generally ignored and you need a "<br>" tag to start a new line.

So you can either use "writeln" for all your output, or you need <pre>, or you need to insert <br> tags between each line.

Hope that helps.
0
 

Author Comment

by:jean11
Comment Utility
Thanks for the reply
Everything was clear before the follwing statement:
>So you can either use "writeln" for all your output, or you need <pre>, or you need to insert <br> tags between each line.

The above statement was confusing
writeln alone as you confirmed (without <pre> will not work. Right?
<pre> alone will not insert a new line? correct

0
 
LVL 16

Expert Comment

by:sjklein42
Comment Utility
Even if you use writeln you will also need <pre>.  Your choice is between the use of a <pre> block plus writeln, vs the use of <br> tags between each line.  They're both pretty much the same but the choice may depends on whether you want fixed-width font (with <pre>) or variable.
0
 
LVL 9

Assisted Solution

by:CCSOFlag
CCSOFlag earned 62 total points
Comment Utility
jean,

the difference is <pre> pays attention to white space and carriage returns in the text.  Normally text on an html page ignores multiple spaces and carriage returns.  writeln adds a carriage return not a <br /> tag.  Thus if you do not have the <pre> around it the carriage return will be removed which makes it just like a write statement.  On the flip side, if you use write in a <pre> tag it will just be one continuous line because write does not input carriage returns.  writeln is mainly used for writing to files rather than writing to the screen.
0
 

Author Comment

by:jean11
Comment Utility
thanks
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now