一、基础知识 1 创建脚本块 1: 2 隐藏脚本代码 1: <script language=”JavaScript”> 2:
5: </script> 在不支持JavaScript的浏览器中将不执行相关代码 3 浏览器不支持的时候显示 1:
4 链接外部脚本文件 1: 5 注释脚本 1: // This is a comment 2: document.write(“Hello”); // This is a comment 3: /* 4: All of this 5: is a comment 6: */ 6 输出到浏览器 1: document.write(“
Hello”); 7 定义变量 1: var myVariable = “some value”; 8 字符串相加 1: var myString = “String1” + “String2”; 9 字符串搜索 1: <script language=”JavaScript”> 2:
7: </script> 10 字符串替换 1: thisVar.replace(“Monday”,”Friday”); 11 格式化字串 1: <script language=”JavaScript”> 2:
21: </script> 12 创建数组 1: <script language=”JavaScript”> 2:
11: </script> 13 数组排序 1: <script language=”JavaScript”> 2:
11: </script> 14 分割字符串 1: <script language=”JavaScript”> 2:
10: </script> 15 弹出警告信息 1: <script language=”JavaScript”> 2:
5: </script> 16 弹出确认框 1: <script language=”JavaScript”> 2:
5: </script> 17 定义函数 1: <script language=”JavaScript”> 2:
8: </script> 18 调用JS函数 1:
Link text 2:
Link text 19 在页面加载完成后执行函数 1: 2: Body of the page 3: 20 条件判断 1: <script> 2:
7: </script> 21 指定次数循环 1: <script> 2:
11: </script> 22 设定将来执行 1: <script> 2:
8: </script> 23 定时执行函数 1: <script> 2:
9: </script> 24 取消定时执行 1: <script> 2:
9: </script> 二:浏览器输出 26 访问document对象 1: 27 动态输出HTML 1: <script language=”JavaScript”> 2: document.write(“
The Main Page 7: 8: 9:
2:
Width 3: 37 动态加载图像 1: 38 简单的图像替换 1: 7:
9:
39 随机显示图像 1: <script language=”JavaScript”> 2: var imageList = new Array; 3: imageList[0] = “image1.jpg”; 4: imageList[1] = “image2.jpg”; 5: imageList[2] = “image3.jpg”; 6: imageList[3] = “image4.jpg”; 7: var imageChoice = Math.floor(Math.random() * imageList.length); 8: document.write(‘
’); 9: </script> 40 函数实现的图像替换 1: 14:
16:
17: 41 创建幻灯片 1: <script language=”JavaScript”> 2: var imageList = new Array; 3: imageList[0] = new Image; 4: imageList[0].src = “image1.jpg”; 5: imageList[1] = new Image; 6: imageList[1].src = “image2.jpg”; 7: imageList[2] = new Image; 8: imageList[2].src = “image3.jpg”; 9: imageList[3] = new Image; 10: imageList[3].src = “image4.jpg”; 11: function slideShow(imageNumber) { 12: document.slideShow.src = imageList[imageNumber].src; 13: imageNumber += 1; 14: if (imageNumber < imageList.length) { 15: window.setTimeout(“slideShow(“ + imageNumber + “)”,3000); 16: } 17: } 18: </script> 19: 20: 21:
42 随机广告图片 1: <script language=”JavaScript”> 2: var imageList = new Array; 3: imageList[0] = “image1.jpg”; 4: imageList[1] = “image2.jpg”; 5: imageList[2] = “image3.jpg”; 6: imageList[3] = “image4.jpg”; 7: var urlList = new Array; 8: urlList[0] = “http://some.host/”; 9: urlList[1] = “http://another.host/”; 10: urlList[2] = “http://somewhere.else/”; 11: urlList[3] = “http://right.here/”; 12: var imageChoice = Math.floor(Math.random() * imageList.length); 13: document.write(‘
’); 14: </script> 四:表单 43 表单构成 1:
44 访问表单中的文本框内容 1:
4:
Check Text Field 45 动态复制文本框内容 1:
5:
Copy Text Field 46 侦测文本框的变化 1:
47 访问选中的Select 1:
8:
Check Selection List 48 动态增加Select项 1:
7: 49 验证表单字段 1: 9:
50 验证Select项 1: function checkList(selection) { 2: if (selection.length == 0) { 3: window.alert(“You must make a selection from the list.”); 4: return false; 5: } 6: return true; 7: } 51 动态改变表单的action 1:
52 使用图像按钮 1:
6: 53 表单数据的加密 1: <script LANGUAGE='JavaScript'> 2:
17: </script> 18:
五:窗口和框架 54 改变浏览器状态栏文字提示 1: 55 弹出确认提示框 1: 56 提示输入 1: 57 打开一个新窗口 1: //打开一个名称为myNewWindow的浏览器新窗口 2: 58 设置新窗口的大小 1: 59 设置新窗口的位置 1: 60 是否显示工具栏和滚动栏 1:
Here’s some information about this document:
”); 3: document.write(“- ”); 4: document.write(“
- Referring Document: “ + document.referrer + “
- ”); 5: document.write(“
- Domain: “ + document.domain + “
- ”); 6: document.write(“
- URL: “ + document.URL + “
- ”); 7: document.write(“
This is a New Document.
”); 5: document.close(); 6: } 7: </script> 34 页面跳转 1: 35 添加网页加载进度窗口 1: 2: 3: 6:This is the main page
10: 11: 三:图像 36 读取图像属性 1:![](/”image1.jpg)
![](/”image1.jpg)
![](/”image1.jpg)
![](/”image1.jpg)