Javascript Reference
Categories: document

javascript document write( ), writeln( )

@March 15, 2008, 1:22 a.m.
write( ), writeln( ) Firefox/Netscape/NN 2 IE 3 Chrome/Safari/DOM n/a

write("string1"[, ..."stringN"]) writeln("string1"[, ..."stringN"])

When invoked as the page loads, these methods can dynamically add content to the page. When invoked after the page has loaded, a single method invocation clears the current document, opens a new output stream, and writes the content to the window or frame. A document.close( ) method is required afterward. Because the first document.write( ) or document.writeln( ) method destroys the current document, do not use two or more writing statements to create a new document. Instead, load the content into one variable and pass that variable as the parameter to a single document.write( ) or document.writeln( ) method.


Using document.write( ) for <script> tags is tricky in Navigator because it typically interprets the writing of the end script tag as meaning the end of the script doing the writing. You should have success, however, if you split the end script tag into string sections:

document.write(<" + "/script>");"

If you include the "hide script" comment trick, write it this way:

document.write("//--" + >");"

The difference between the two methods is that document.writeln( ) adds a carriage return to the source code it writes to the document. This is not reflected in the rendered content, but can make reading the dynamic source code easier in browser versions that support dynamic content source viewing (Navigator 4 does so as a wysiwyg: URL in the source view window).


Any string value, including HTML tags.

Returned Value


difference between document.write and document.writeln
shree - 2008-11-22

Powered by Linode.