Solved

document.write vs document.writeln

Posted on 2011-02-14
5
603 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

808 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