21 指定次數循環
1: <script>
2: <!--
3: var myarray = new array(3);
4: myarray[0] = “item 0”;
5: myarray[1] = “item 1”;
6: myarray[2] = “item 2”;
7: for (i = 0; i < myarray.length; i++) {
8: document.write(myarray + “<br>”);
9: }
10: // -->
11: </script>
22 設定将來執行
3: function hello() {
4: window.alert(“hello”);
5: }
6: window.settimeout(“hello()”,5000);
7: // -->
8: </script>
23 定時執行函數
5: window.settimeout(“hello()”,5000);
6: }
7: window.settimeout(“hello()”,5000);
8: // -->
9: </script>
24 取消定時執行
6: var mytimeout = window.settimeout(“hello()”,5000);
7: window.cleartimeout(mytimeout);
25 在頁面解除安裝時候執行函數
1: <body onunload=”functionname();”>
2: body of the page
3: </body>
javascript就這麼回事2:浏覽器輸出
26 通路document對象
1: <script language=”javascript”>
2: var myurl = document.url;
3: window.alert(myurl);
4: </script>
27 動态輸出html
2: document.write(“<p>here’s some information about this document:</p>”);
3: document.write(“<ul>”);
4: document.write(“<li>referring document: “ + document.referrer + “</li>”);
5: document.write(“<li>domain: “ + document.domain + “</li>”);
6: document.write(“<li>url: “ + document.url + “</li>”);
7: document.write(“</ul>”);
28 輸出換行
1: document.writeln(“<strong>a</strong>”);
2: document.writeln(“b”);
29 輸出日期
2: var thisdate = new date();
3: document.write(thisdate.tostring());
30 指定日期的時區
2: var myoffset = -2;
3: var currentdate = new date();
4: var useroffset = currentdate.gettimezoneoffset()/60;
5: var timezonedifference = useroffset - myoffset;
6: currentdate.sethours(currentdate.gethours() + timezonedifference);
7: document.write(“the time and date in central europe is: “ + currentdate.tolocalestring());