A+ A-

The document.writeln(string) command.

The document.writeln(string) command.


This can be used while the page is being constructed. After the page has finished loading a new
document.writeln(string) command will delete the page in most browsers, so use this only while the page is loading. Here's how a simple web-page will look...


[code type="JavaScript"]<html>
<head>
</head>
<body>
<script type="text/javascript">
document.writeln('Hello World!');
</script>
</body>
</html>[/code]


As the page is loading, Javascript will encounter this script and it will output "Hello World!" exactly where the script block appears on the page. The problem with writeln is that if you use this method after the page has loaded the browser will destroy the page and start constructing a new one. For the most part, document.writeln is useful only when teaching yourself the language. Dynamic content during page load is better served by the server-side scripting languages. That said, document.writeln is very useful in pre-processing forms before they're sent to the server -- you can basically create a new web-page on the fly without the need to contact the server.

It should also be noted, before we begin, that Javascript is extremely case sensitive so if you're trying to code along with any examples make sure lowercase is lowercase and uppercase is uppercase. For the most part Javascript is also a camel-cased language. That is, if you're trying to express more than one word you will eliminate the spaces, leave the first letter uncapitalized and capitalize the first letter of each word. Thus "get element by id" becomes "getElementByID".