一 页面输出
1.头部文件
2.页面内
<script> <BR>document.write("脚本之家"); <BR></script>
3.外部文件
4.利用页面ID的innerHtml
<script> <BR>window.onload = writeMessage; // 页面加载时调用writeMessage函数 <BR>writeMessage() { <BR>document.getElementById("helloMessage").innerHTML = "脚本之家"; <BR>//找到dom ID(helloMessage),修改其html内容 <BR>} <BR></script>
5.警告
alert("广州百汇物流有限公司");
6.询问
if (confirm("是否访问我们的首页"))
{
alert("是的,前往");
}
else {
alert("退出");
}
7.输入
var ans = prompt("输入你的留言","您好,");
if (ans) {
alert("你说:" + ans);
}
else {
alert("退出,没有留言");
}
8.页面跳转
<script> <BR>window.onload = initAll; <BR>function initAll() { <BR>document.getElementById("redirect").onclick = initRedirect; <BR>} <BR>function initRedirect() <BR>{ <BR>window.location = "index.html"; <BR>return false; <BR>} <BR></script>
脚本之家
9.判断分支
<script> <BR>window.onload = initAll; <BR>function initAll() { <BR>document.getElementById("Lincoln").onclick = saySomething; <BR>document.getElementById("Kennedy").onclick = saySomething; <BR>document.getElementById("Nixon").onclick = saySomething; <BR>} <BR>function saySomething() { <BR>switch(this.id) { <BR>case "Lincoln": <BR>alert("Four score and seven years ago..."); <BR>break; <BR>case "Kennedy": <BR>alert("Ask not what your country can do for you..."); <BR>break; <BR>case "Nixon": <BR>alert("I am not a crook!"); <BR>break; <BR>default: <BR>} <BR>} <BR></script>
10.异常捕获
window.onload = initAll;
function initAll() {
var ans = prompt("输入参数:","");
try {
if (!ans || isNaN(ans) || ansthrow new Error("输入为非数");
}
alert("根号" + ans + " 是 " + Math.sqrt(ans));
}
catch (errMsg) {
alert(errMsg.message);
}
}