客户端代码是顺序执行的。 我支持楼顶的。自己用下面的代码试试: <html> <head> <title>title</title> <script> var buttonNotPressed1=setTimeout("alert('you must press ok button to continue, This is No1')",3000);function buttonNotPressed2(){ setTimeout("alert('you must press ok button to continue, this is NO2')",3000); } </script> </head> <body onload="openpolarbear2()"> </body> </html>
你的函数名写错了,我也粘贴错了,下面正确:<html> <head> <title>title</title> <script language=javascript> var buttonNotPressed1=setTimeout("alert('you must press ok button to continue, This is No1')",1000); function buttonNotPressed2(){ setTimeout("alert('you must press ok button to continue, this is NO2')",1000); } </script> </head> <body onload="buttonNotPressed2();"> </body> </html>
我支持楼顶的。自己用下面的代码试试:
<html>
<head>
<title>title</title>
<script>
var buttonNotPressed1=setTimeout("alert('you must press ok button to continue, This is No1')",3000);function buttonNotPressed2(){
setTimeout("alert('you must press ok button to continue, this is NO2')",3000);
}
</script>
</head>
<body onload="openpolarbear2()">
</body>
</html>
<head>
<title>title</title>
<script language=javascript>
var buttonNotPressed1=setTimeout("alert('you must press ok button to continue, This is No1')",1000);
function buttonNotPressed2(){
setTimeout("alert('you must press ok button to continue, this is NO2')",1000);
}
</script>
</head>
<body onload="buttonNotPressed2();">
</body>
</html>
虽然这两种写法运行效果是一样的,但是其运行的条件与时间顺序是不同的.第一句代码是当浏览器加载到这句语句时就运行了这句,而onload事件里加载函数是当所有的HTML代码流全部被加载完毕以后再执行的一个事件,所以第一句代码运行在第二句函数之前.另外,第一句运行的时候,其后面的代码还未被浏览器所加载,所以你对后面的对象进行操作时就会报该对象不存在的错误,因为此时这些代码都还未被浏览器加载,但在执行onload事件时,所有的代码都是已经被加载完毕的,所以在onload对网页的对象进行操作时就不会出错.你可以看到下面这个例子,就可以看出这些的不同.<script language=javascript>
alert("aaaa");
document.all.txt.value="赋值时会出错";
function cc(){alert("bbbbb");document.all.txt.value="meizz";}
</script><body bgcolor=#00ff00 onload=cc()>
<input name=txt>但是我将这些JS代码放在该对象出现之后,就不会出现错误,原因是在JS语句运行时该对象已经被浏览器加载了.<body bgcolor=#00ff00 onload=cc()>
<input name=txt><script language=javascript>
alert("aaaa");
document.all.txt.value="赋值时会出错";
function cc(){alert("bbbbb");document.all.txt.value="meizz";}
</script>