11 格式化字串
1: <script language=”javascript”>
2: <!--
3: var myvariable = “hello there”;
4: document.write(myvariable.big() + “<br>”);
5: document.write(myvariable.blink() + “<br>”);
6: document.write(myvariable.bold() + “<br>”);
7: document.write(myvariable.fixed() + “<br>”);
8: document.write(myvariable.fontcolor(“red”) + “<br>”);
9: document.write(myvariable.fontsize(“18pt”) + “<br>”);
10: document.write(myvariable.italics() + “<br>”);
11: document.write(myvariable.small() + “<br>”);
12: document.write(myvariable.strike() + “<br>”);
13: document.write(myvariable.sub() + “<br>”);
14: document.write(myvariable.sup() + “<br>”);
15: document.write(myvariable.tolowercase() + “<br>”);
16: document.write(myvariable.touppercase() + “<br>”);
17:
18: var firststring = “my string”;
19: var finalstring = firststring.bold().tolowercase().fontcolor(“red”);
20: // -->
21: </script>
12 创建数组
3: var myarray = new array(5);
4: myarray[0] = “first entry”;
5: myarray[1] = “second entry”;
6: myarray[2] = “third entry”;
7: myarray[3] = “fourth entry”;
8: myarray[4] = “fifth entry”;
9: var anotherarray = new array(“first entry”,”second entry”,”third entry”,”fourth entry”,”fifth entry”);
10: // -->
11: </script>
13 数组排序
4: myarray[0] = “z”;
5: myarray[1] = “c”;
6: myarray[2] = “d”;
7: myarray[3] = “a”;
8: myarray[4] = “q”;
9: document.write(myarray.sort());
14 分割字符串
3: var myvariable = “a,b,c,d”;
4: var stringarray = myvariable.split(“,”);
5: document.write(stringarray[0]);
6: document.write(stringarray[1]);
7: document.write(stringarray[2]);
8: document.write(stringarray[3]);
9: // -->
10: </script>
15 弹出警告信息
3: window.alert(“hello”);
4: // -->
5: </script>
16 弹出确认框
3: var result = window.confirm(“click ok to continue”);
17 定义函数
3: function multiple(number1,number2) {
4: var result = number1 * number2;
5: return result;
6: }
7: // -->
8: </script>
18 调用js函数
1: <a href=”#” onclick=”functionname()”>link text</a>
2: <a href="/”javascript:functionname"()”>link text</a>
19 在页面加载完成后执行函数
1: <body onload=”functionname();”>
2: body of the page
3: </body>
20 条件判断
1: <script>
3: var userchoice = window.confirm(“choose ok or cancel”);
4: var result = (userchoice == true) ? “ok” : “cancel”;
5: document.write(result);
6: // -->
7: </script>