1.onload——页面加载完成时执行
<html>
<head>
<script>
function message()
{
alert("该提示框是通过 onload 事件调用的。")
}
</script>
</head>
<body οnlοad="message()">
</body>
</html>
2. document.write()——向页面写入元素
<body >
<p>hello</p>
<script>
document.write("it is test01");
document.write("hello"); //这两句之间没有人任何分隔符,<span style="font-family: Simsun;font-size:14px;">it is test01hello</span>
document.write("<h2>it is test02</h2>"); //以h2的格式展出这些内容
var firstname;
firstname="George";
document.write(firstname);//输出变量
document.write("<br />"); //输出换行
function product(a,b)
{
return a*b
}
document.write(product(6,5));
</script>
</body>
3. for/in 语句循环遍历对象的属性,遍历数组
<html>
<body>
<script type="text/javascript">
var x
var mycars = new Array()
mycars[0] = "宝马"
mycars[1] = "奔驰"
mycars[2] = "宾利"
for (x in mycars)
{
document.write(mycars[x] + "<br />")
}
</script>
</body>
</html>
4.throw
<html>
<body>
<script type="text/javascript">
var x=prompt("请输入 0 至 10 之间的数:","")
try
{
if(x>10)
throw "Err1" <strong>//er=Err1</strong>
else if(x<0)
throw "Err2"
else if(isNaN(x))
throw "Err3"
}
catch(er)
{
if(er=="Err1")
alert("错误!该值太大!")
if(er == "Err2")
alert("错误!该值太小!")
if(er == "Err3")
alert("错误!该值不是数字!")
}
</script>
</body>
</html>
<html>
<head>
<script type="text/javascript">
<strong>οnerrοr=handleErr</strong>
var txt=""
function handleErr(<strong>msg</strong>,<strong>url</strong>,<strong>l</strong>) //系统变量
{
txt="本页中存在错误。\n\n"
txt+="错误:" + msg + "\n"
txt+="URL: " + url + "\n"
txt+="行:" + l + "\n\n"
txt+="点击“确定”继续。\n\n"
alert(txt);
return true;
}
function message()
{
adddlert("Welcome guest!")
}
</script>
</head>
<body>
<input type="button" value="查看消息" οnclick="message()" />
</body>
</html>
<html>
<head>
<script type="text/javascript">
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=")
if (c_start!=-1)
{
c_start=c_start + c_name.length+1
c_end=document.cookie.indexOf(";",c_start)
if (c_end==-1) c_end=document.cookie.length
return unescape(document.cookie.substring(c_start,c_end))
}
}
return ""
}
function setCookie(c_name,value,expiredays)
{
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : "; expires="+exdate.toGMTString())
}
function checkCookie()
{
username=getCookie('username')
if (username!=null && username!="")
{
alert('Welcome again '+username+'!')
}
else
{
username=prompt('Please enter your name:',"")
if (username!=null && username!="")
{
setCookie('username',username,365)
}
}
}
</script>
</head>
<body onLoad="checkCookie()">
</body>
</html>
7.setTimeout()
setTimeout(<strong>"alert('3秒')"</strong>,3000); //3秒以后弹出警告框
8.从0开始计时功能
<html>
<head>
<script type="text/javascript">
var c=0
var t
function timedCount()
{
document.getElementById('txt').value=c
c=c+1
setTimeout("timedCount()",1000)
}
</script>
</head>
<body>
<form>
<input type="button" value="开始计时!" onClick="timedCount()">
<input type="text" id="txt">
</form>
<p>请点击上面的按钮。输入框会从 0 开始一直进行计时。</p>
</body>
</html>
9.页面钟表功能
<html>
<head>
<script type="text/javascript">
function startTime()
{
var today=new Date()
var h=today.getHours()
var m=today.getMinutes()
var s=today.getSeconds()
// add a zero in front of numbers<10
m=checkTime(m)
s=checkTime(s)
document.getElementById('txt').innerHTML=h+":"+m+":"+s
setTimeout('startTime()',500)
}
function checkTime(i)
{
if (i<10)
{i="0" + i}
return i
}
</script>
</head>
<body οnlοad="startTime()">
<div id="txt"></div>
</body>
</html>