Click to See Complete Forum and Search --> : Newbie question about onload function


Sunwins
08-22-2003, 03:33 PM
I'm trying to learn JavaScript from a book (JavaScript Unleashed by Wagner and Wyke).

If I use the this code:

<html>
<head>
<title>hello</title>
</head>

<body onload="PrintText()">

Hello from hello.htm<br>

<script language="JavaScript">
document.write("test 1 of document.write() statement<br>")
</script>

<script language="JavaScript">
function PrintText(){
alert("JavaScript test")
}
</script>

<script language="JavaScript">
document.write("test 2 of document.write() statement<br>");
document.write("test 3 of document.write() statement<br>");
</script>

</body>
</html>

I get a popup box saying "JavaScript test" and the following is printed on the page:

Hello from hello.htm
test 1 of document.write() statement
test 2 of document.write() statement
test 3 of document.write() statement


But, if I change the PrintText function to this:

<script language="JavaScript">
function PrintText(){
document.write("output of PrintText")
}
</script>

Then it briefly prints the text:

Hello from hello.htm
test 1 of document.write() statement

and then clears the screen and prints the text:

output of PrintText


What's causing the screen to be cleared?

David Harrison
08-22-2003, 03:44 PM
When you call the function onload, the page has already fnished loading, which means that the page source will be completely replaced by:

output of PrintText

You can't use document.write after onload (unless you're using layers I think).

Jupac
08-22-2003, 08:54 PM
I think the function should be before the onload ("in the head")

Sunwins
08-22-2003, 09:44 PM
I moved the function to the head section and it didn't make any difference.

Jupac
08-22-2003, 10:01 PM
try this
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<!--
function text(){
var msg="HI my users"
document.writeln(""+msg+"");
}
// -->
</script>
<title>Test</title>
</head>
<body onload=text()>

</body>
</html>

AdamBrill
08-22-2003, 10:03 PM
If you use document.write after the document is closed(ie. after the page is loaded), it will overwrite everything that is in the document to write in the new text. This example might help you:<html>
<head>
<title>hello</title>
</head>

<body onload="PrintText()">

Hello from hello.htm<br>

<script type="text/javascript">
function PrintText(){
document.getElementById('div_id').innerHTML="JavaScript test";
}
document.write("test 1 of document.write() statement<br>");
document.write("test 2 of document.write() statement<br>");
document.write("test 3 of document.write() statement<br>");
</script>
<div id="div_id"></div>
</body>
</html>

BTW, lakers01, your script did not fix the problem... You just couldn't tell that is wasn't working since you didn't put anything in the body. ;)

Jupac
08-22-2003, 10:45 PM
O LOL!!

Sunwins
08-25-2003, 10:26 AM
Thanks for you help everyone!