設置按期間隔
<script>
window.setInterval("document.form1.text2.value=document.form1.text1.value",3000)
</script>
<form name=form1>
<input type=text name=text1><br>
<input type=text name=text2><br>
</form>
清除按期間隔
<script>
stop=window.setInterval("document.form1.text2.value=document.form1.text1.value",300)
</script>
<form name=form1>
<input type=text name=text1><br>
<input type=text name=text2><br>
<input type=button name=button1 value=" 清除按期間隔" onclick=clearInterval(stop)>
</form>
從函數返回值
<script>
function average(var1,var2,var3)
{ave=(var1+var2+var3)/3;
document.write("輸出結果");
return ave;
}
document.write(average(34,56,78))
</script>
經過HTML連接調用函數
<script>
function quote(){
document.write(" 輸出字符串")
}
</script>
<a href=javascript:quote()>經過HTML連接調用函數</a>
<a href=javascript:Document.write("輸出字符")> 經過HTML連接調用函數,直接寫javascript語句</a>
檢測按鍵
<form name=form1>
<input type=text name=text1 value=hello onkeypress="if(window.event.keyCode=='100') document.form1.text1.value='你按了d鍵'">
</form>
建立網頁加載和卸載信息
<body onload=document.form1.text1.value='頁面加載完畢' onunload=alert('再見,歡迎再來')>
<form name=form1>
<input type=text name=text1 value="頁面正在加載 ……">
</form>
預加載圖像
<script>
freddy=new Image()
freddy.src=freddy.gif
</script>
<body onload=document.p_w_picpaths[0].src=freddy.src>
,<img src="blank.gif">
</body>
使用link和anchor對象
<a name=anchor1>錨點1<br>
<a href=http://www.microsoft.com>Microsoft</a><br>
<a href=http://www.sohu.com>sohu</a><br>
<a href=http://www.sina.com.cn>sina</a><br>
<script>
document.write("本頁面共有"+document.links.length+"連接"+"<br>")
document.write("本頁面共有"+document.anchors.length+"錨點"+"<br>")
document.write("第一個連接協議是"+document.links[0].protocol+"<br>")
document.write("第一個連接路徑是"+document.links[0].pathnamel+"<br>")
document.write("第一個連接href是"+document.links[0].hrefl+"<br>")
</script>
改變連接
<a href =http://www.microsoft.com>link</a>
<form name=form1>
<input type=button name=button1 value="改變連接" onclick=document.links[0].href='http://www.sohu.com'>
</form>
顯示修改日期
<script>
document.write("本頁面最後修改時間是"+document.lastModified)
</script>
查看當前文檔的URL
查看引用頁
<script>
document.write("本頁面的引用頁是"+document.referrer)
</script>
移動窗口
<form name=form1>
水平方向<input type=text name=x value=20>
垂直方向<input type=text name=y value=50>
<input type=button value="移動窗口到…"onclick=window.moveTo(document.form1.x.value,document.form1.y.value)>
</form>
<form name=form1>
水平方向<input type=text name=x value=20>
垂直方向<input type=text name=y value=50>
<input type=button value="移動窗口"onclick=window.moveBy(document.form1.x.value,document.form1.y.value)>
</form>
改變窗口大小
<form name=form1>
水平方向<input type=text name=x value=200>
垂直方向<input type=text name=y value=500>
<input type=button value="改變窗口大小到….."onclick=window.resizeTo(document.form1.x.value,document.form1.y.value)>
</form>
<form name=form1>
水平方向<input type=text name=x value=200>
垂直方向<input type=text name=y value=500>
<input type=button value="改變窗口大小"onclick=window.resizeBy(document.form1.x.value,document.form1.y.value)>
</form>
使用隱藏字段
<form name=form1>
<input type=hidden name=hid1 value=piece of eight>
</form>
<script>
document.write("表單hid1的類型:"+document.form1.hid1.type+"<br>")
document.write("表單hid1的名稱:"+document.form1.hid1.name+"<br>")
document.write("表單hid1的值:"+document.form1.hid1.value+"<br>")
使用navigator對象
<script>
document.write("navigator對象的屬性"+"<br>")
document.write("appcodename:"+navigator.appCodeName+"<br>")
document.write("appname::"+navigator.appName+"<br>")
document.write("appversion:"+navigator.appVersion+"<br>")
document.write("platform:"+navigator.platform+"<br>")
document.write("userAgent:"+navigator.userAgent+"<br>")
</script>
<script>
document.write("navigator對象的方法"+"<br>")
document.write("javaEnabled():"+navigator.javaEnabled())
</script>
檢查用戶的瀏覽器
<script>
if(navigator.appName.indexOf("Microsoft")!=-1){
document.write("用戶瀏覽器是微軟的IE瀏覽器"+"<br>")}
else if(navigator.appName.indexOf("Netscape")!=-1){
document.write("用戶瀏覽器是netscape的netscape瀏覽器"+"<br>")}
if(navigator.appVersion.indexOf("4.0")!=-1){
document.write("you are using a version 4.0compatible browser")
}
else{
document.write("this browser is not 4.0 compliant")}
</script>
檢測用戶的操做系統
<script>
if (navigator.platform.indexOf("win32")!=-1){
document.write("you are using a computer running windows 95 or highter")}
else{
document.write("this computer is not running windows 95 or higher")}
</script>
使用cookie
<script>
finction makecookie(){
if(!document.cookie){
name=prompt("請輸入你的姓名");
document.cookie="name="+name+";";}
}
</script>
<body onload=makecookie()><script>function makecookie(){if(!document.cookie){name=prompt("請輸入你的姓名")document.cookie="name="+name+";";namestart=document.cookie.indexOf("="); nameend=document.cookieindexOf(";");document.writeln("your name is:"+document.cookie.substring(namestart+1,nameend)+",br>")}}</script>