Writing to a page on loading

Writing with JavaScript 1

This is a new series of pages about document.write. You will learn about 3 situations:

  1. Writing to a document when it loads
  2. Writing to a document when it has already loaded and replacing the original document
  3. Writing to a document when it has already load, but only adding the new code and retaining the original document.

This page is about loading a document when it loads. Much of this series has been superseded by new functions in JavaScript. modifying existing text in JavaScript2.

The following was written by JavaScript (the stuff between the two horizontal line:



Actually, whatever it says, it didn't write it all by itself, because I wrote it, but JavaScript did copy what I wrote to the page. Having got this straight, you can look at the code in the HEAD of the document.

This code is a function that defines what you want JavaScript to write.

The key part of the function is:

document.write(myMessage);

The function PartPageWriter() writes the new text.

We can get some writing done by JavaScript by putting the function in the document where we want the writing done. So we put this in the document:

<script type="text/javascript">

<!--//

PartPageWriter()

//-->

</script>

PartPageWriter().

So to get JavaScript to write something in a page when it is first loaded, we make a function that defines the message we want to display (in HTML) and then put the function name in the page where we want to do our writing.

Here is the above example as a full page:

<html>

<head>

<!--start of function-->

<script type="text/javascript">

<!--//

function PartPageWriter(){

//Here is the bit which writes the message

myMessage='<table border=10><tr><td>Although '+

'it doesn\'t seem much, I wrote this all by myself<p>By'+ 'JavaScript</td><\tr></table>';

document.write(myMessage);

}

//-->

</script>

<!--end of function-->

</head>

<body>

<script type="text/javascript">

<!--//

//By writing the function, the result is performed here - it writes the message

PartPageWriter()

//-->

</script>

</body>

</html>

  The next example in the document.write series shows you how to create a complete new page with document.write.

Next: Writing a new page.

 

Ken Ward's HTML Guide...

 


[If you need to learn more about HTML visit the HTML Tutorial]

To ask questions, make comments, etc,
use the mailing list

I am always pleased to hear from you.
To ask questions, make comments, etc,
use the mailing list : Click here to join 1stJavaScript