?
Solved

document.write vs document.writeln

Posted on 2011-02-14
5
Medium Priority
?
641 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 16

Accepted Solution

by:
sjklein42 earned 252 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 248 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…
Suggested Courses

801 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