Solved

document.write vs document.writeln

Posted on 2011-02-14
5
600 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
ID: 34891785
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
ID: 34891857
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
ID: 34891891
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
ID: 34892414
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
ID: 34945933
thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

920 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

16 Experts available now in Live!

Get 1:1 Help Now