创建脚本块
1<script language=”JavaScript”>
2JavaScript code goes here
3</script>
隐藏脚本代码
1<script language=”JavaScript”>
2<!--
3document.write(“Hello”);
4-->
5</script>
浏览器不支持的时候显示
1<noscript>
2Hello to the non-JavaScript browser.
3</noscript>
链接外部脚本文件
1<script language=”JavaScript” src="/”filename.js"”></script>
注释脚本
1// This is a comment
2 document.write(“Hello”); // This is a comment
3 /*
4 All of this
5 is a comment
6 */
输出到浏览器
1document.write(“<strong>Hello</strong>”);
定义变量
1var myVariable = “some value”;
字符串相加
1var myString = “String1” + “String2”;
字符串搜索
1<script language=”JavaScript”>
2 var myVariable = “Hello there”;
3 var therePlace = myVariable.search(“there”);
4 document.write(therePlace);
5</script>
字符串替换
1<script language=”JavaScript”>
2 thisVar.replace(“Monday”,”Friday”);
3</script>
格式化字串
1<script language=”JavaScript”>
2 var myVariable = “Hello there”;
3 document.write(myVariable.big() + “<br>”);
4 document.write(myVariable.blink() + “<br>”);
5 document.write(myVariable.bold() + “<br>”);
6 document.write(myVariable.fixed() + “<br>”);
7 document.write(myVariable.fontcolor(“red”) + “<br>”);
8 document.write(myVariable.fontsize(“18pt”) + “<br>”);
9 document.write(myVariable.italics() + “<br>”);
10 document.write(myVariable.small() + “<br>”);
11 document.write(myVariable.strike() + “<br>”);
12 document.write(myVariable.sub() + “<br>”);
13 document.write(myVariable.sup() + “<br>”);
14 document.write(myVariable.toLowerCase() + “<br>”);
15 document.write(myVariable.toUpperCase() + “<br>”);
16 var firstString = “My String”;
17 var finalString = firstString.bold().toLowerCase().fontcolor(“red”);
18</script>
创建数组
1<script language=”JavaScript”>
2 var myArray = new Array(5);
3 myArray[0] = “First Entry”;
4 myArray[1] = “Second Entry”;
5 myArray[2] = “Third Entry”;
6 myArray[3] = “Fourth Entry”;
7 myArray[4] = “Fifth Entry”;
8 var anotherArray = new Array(“First Entry”,”Second Entry”,”Third Entry”,”Fourth Entry”,”Fifth Entry”);
9</script>
数组排序
1<script language=”JavaScript”>
2 var myArray = new Array(5);
3 myArray[0] = “z”;
4 myArray[1] = “c”;
5 myArray[2] = “d”;
6 myArray[3] = “a”;
7 myArray[4] = “q”;
8 document.write(myArray.sort());
9</script>
分割字符串
1<script language=”JavaScript”>
2 var myVariable = “a,b,c,d”;
3 var stringArray = myVariable.split(“,”);
4 document.write(stringArray[0]);
5 document.write(stringArray[1]);
6 document.write(stringArray[2]);
7 document.write(stringArray[3]);
8</script>
弹出警告信息
1<script language=”JavaScript”>
2 window.alert(“Hello”);
3</script>
弹出确认框
1<script language=”JavaScript”>
2 var result = window.confirm(“Click OK to continue”);
3</script>
定义函数
1<script language=”JavaScript”>
2 function multiple(number1,number2) {
3 var result = number1 * number2;
4 return result;
5 }
6</script>
调用JS函数
1<a href=”#” onClick=”functionName()”>Link text</a>
2<a href="/”javascript:functionName"()”>Link text</a>
在页面加载完成后执行函数
1<body onLoad=”functionName();”>
2Body of the page
3</body>
条件判断
1<script>
2 var userChoice = window.confirm(“Choose OK or Cancel”);
3 var result = (userChoice == true) ? “OK” : “Cancel”;
4 document.write(result);
5</script>
指定次数循环
1<script>
2 var myArray = new Array(3);
3 myArray[0] = “Item 0”;
4 myArray[1] = “Item 1”;
5 myArray[2] = “Item 2”;
6 for (i = 0; i < myArray.length; i++) {
7 document.write(myArray[i] + “<br>”);
8 }
9</script>
设定将来执行
1<script>
2 function hello() {
3 window.alert(“Hello”);
4 }
5 window.setTimeout(“hello()”,5000);
6</script>
定时执行函数
1<script>
2 function hello() {
3 window.alert(“Hello”);
4 window.setTimeout(“hello()”,5000);
5 }
6 window.setTimeout(“hello()”,5000);
7</script>
取消定时执行
1<script>
2 function hello() {
3 window.alert(“Hello”);
4 }
5 var myTimeout = window.setTimeout(“hello()”,5000);
6 window.clearTimeout(myTimeout);
7</script>
在页面卸载时候执行函数
1<body onUnload=”functionName();”>
2 Body of the page
3</body>
posted on 2009-01-11 10:21
飞翔天使 阅读(249)
评论(0) 编辑 收藏 所属分类:
javascript