Order of execution

C

chirs

I have a small code. It prints 0,1,...,9, but you cannot see the word
"hi". It seems that it prints "hi" then executes the listnum
function. But the listnum function is placed before "hi". Could you
give me some info on order of execution?

<HEAD>
<SCRIPT language=JavaScript>
function listnum() {
i=0
do {
document.write( i + "<br>")
i++
} while(i<10)
}</SCRIPT>
</HEAD>

<BODY onload=listnum()>
hi
</BODY>

Thank you.
 
V

Vjekoslav Begovic

chirs said:
I have a small code. It prints 0,1,...,9, but you cannot see the word
"hi". It seems that it prints "hi" then executes the listnum
function. But the listnum function is placed before "hi". Could you
give me some info on order of execution?

<HEAD>
<SCRIPT language=JavaScript>
function listnum() {
i=0
do {
document.write( i + "<br>")
i++
} while(i<10)
}</SCRIPT>
</HEAD>

<BODY onload=listnum()>
hi
</BODY>

The onLoad event handler triggers when a page finishes loading. When you
document.write on such a page, you destroy the previous page. You could try
the following:

<body>
<script type="text/javascript">
listnum();
</script>
hi
</body>

HTH

Vjekoslav
 
J

JB

Try this,
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
</HEAD>
<BODY>
Hi<br>
<SCRIPT language=JavaScript>
{
i=0
do {
document.write( i + "<br>")
i++
} while(i<10)
}</SCRIPT>
</BODY>
</html>
------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top