20条件判断
1:
2:
--
3:
varuserChoice=window.confirm(“ChooseOKorCancel”);
4:
varresult=(userChoice==true)?
“OK”:
“Cancel”;
5:
document.write(result);
6:
//-->
7:
21指定次数循环
1:
2:
--
3:
varmyArray=newArray(3);
4:
myArray[0]=“Item0”;
5:
myArray[1]=“Item1”;
6:
myArray[2]=“Item2”;
7:
for(i=0;i 8: document.write(myArray[i]“ 9: } 10: //--> 11: 22设定将来执行 1: 2: -- 3: functionhello(){ 4: window.alert(“Hello”); 5: } 6: window.setTimeout(“hello()”,5000); 7: //--> 8: 23定时执行函数 1: 2: -- 3: functionhello(){ 4: window.alert(“Hello”); 5: window.setTimeout(“hello()”,5000); 6: } 7: window.setTimeout(“hello()”,5000); 8: //--> 9: 24取消定时执行 1: 2: -- 3: functionhello(){ 4: window.alert(“Hello”); 5: } 6: varmyTimeout=window.setTimeout(“hello()”,5000); 7: window.clearTimeout(myTimeout); 8: //--> 9: 25在页面卸载时候执行函数 1: 2: Bodyofthepage 3: JavaScript就这么回事2: 浏览器输出 26访问document对象 1: 2: varmyURL=document.URL; 3: window.alert(myURL); 4: 27动态输出HTML 1: 2: document.write(“ Here’ssomeinformationaboutthisdocument:
”);
3:
document.write(“
4:
document.write(“
“document.referrer“
5:
document.write(“
“document.domain“
6:
document.write(“
“document.URL“
7:
document.write(“
”);
8:
28输出换行
1:
document.writeln(“a”);
2:
document.writeln(“b”);
29输出日期
1:
2:
varthisDate=newDate();
3:
document.write(thisDate.toString());
4:
30指定日期的时区
1:
2:
varmyOffset=-2;
3:
varcurrentDate=newDate();
4:
varuserOffset=currentDate.getTimezoneOffset()/60;
5:
vartimeZoneDifference=userOffset-myOffset;
6:
currentDate.setHours(currentDate.getHours()timeZoneDifference);
7:
document.write(“ThetimeanddateinCentralEuropeis:
“currentDate.toLocaleString());
8:
31设置日期输出格式
1:
2:
varthisDate=newDate();
3:
varthisTimeString=thisDate.getHours()“:
”thisDate.getMinutes();
4:
varthisDateString=thisDate.getFullYear()“/”thisDate.getMonth()“/”thisDate.getDate();
5:
document.write(thisTimeString“on“thisDateString);
6:
32读取URL参数
1:
2:
varurlParts=document.URL.split(“?
”);
3:
varparameterParts=urlParts[1].split(“&”);
4:
for(i=0;i 5: varpairParts=parameterParts[i].split(“=”); 6: varpairName=pairParts[0]; 7: varpairValue=pairParts[1]; 8: document.write(pairName“: “pairValue); 9: } 10: 你还以为HTML是无状态的么? 33打开一个新的document对象 1: 2: functionnewDocument(){ 3: document.open(); 4: document.write(“ ThisisaNewDocument.
5:
document.close();
6:
}
7:
34页面跳转
1:
2:
window.location=“
3:
35添加网页加载进度窗口
1:
2:
3:
4:
varplaceHolder=window.open('holder.html','placeholder','width=200,height=200');
5:
6:
7:
8:
9:
Thisisthemainpage
10:
11: