<html>
<head>
<script type = "text/javascript">
//创建一个欢迎的cookie
function getCookie(c_name){
if(document.cookie.length > 0){
c_start.document.length.indexof(c_name + "=");
if(c_start != -1){
c_start = c_start + c_name.length + 1;
c_end = document.cookie.indexof(":",c_start);
if( c_end == -1){
c_end = document.cooklie.length;
return unescape(document.cookie.substring(c_start,c_end));
}
}
}
return "";
}
function setCookie(c_name,value,expiredays){
var exDate = new Date();
exDate.setDate(exDate.getDate() + expiredays);
document.cookie = c_name + "=" + escape(value) +((espiredays == null)? "" : "expires = "+exDate.toGMTstring() )
}
function checkCookie(){
if(userName != null && userName != ""){
alert("Welcome again" + userName + "!");
}else{
userName = ("please enter your name:" + "");
if(userName != null && userName != ""){
setCookie("userName",username,365);
}
}
}
//try..catch语句
var txt = "";
function message(){
try{
adddlert("Welcome guest!");
}
catch(err){
txt = "本页中存在错误!\n\n";
txt += "错误描述:"err.description"\n\n";
txt += "点击"确定"按钮继续.\n\n";
alert(txt);
}
}
</script>
//带有确认框的try..catch语句
<script type = "text/javascript">
var txt1 = "";
function message1(){
try{
adddlert("");
}catch(err){
txt = "本页中存在错误。\n\n";
txt += "点击"确定"继续查看本页";
txt += "点击"取消"返回本页";
if(!confirm (txt){
document.location.href = "./index.html";
}
}
}
onerror = handleErr;
var txt = "";
function handleErr(msg,ur1,1){
txt = "本页中存在错误!\n\n";
txt += "错误" + msg + "\n";
txt += "Url" + ur1 + "\n";
txt += "行" + 1 + "\n\n";
txt += "点击"确定"继续。\n\n";
}
function message2(){
adddlert("Welcome guest!");
}
</script>
</head>
<body onload = "detectBrowser()">
<script type = "text/javascript">
//使用for ..in遍历数组内的元素
var x;
var mycars = new Array();
mycars[0] = "宝马";
mycars[1] = "奔驰";
mycars[2] = "宾利";
for( x in mycars){
document.write(mycars [x] + "<br>");
}
</script>
<input type = "button" onclick = "message()" value = "查看消息">
<input type = "button" onclick = "message1()" value = "查看">
<input type = "button" onclick = "message2()" value = "look here">
//throw声明
<script type = "text/javascript">
var x = prompt("请输入0-10之间的整数:","");
try(x > 10){
if(x > 10) throw"err1";
else if(x < 0) throw"err2";
else if(isNaN) throw"err3";
}catch(err){
if(err = = "err1"){
alert("错误!该值太大!");
if(err == "err2"){
alert("错误!该值太小!");
if(err == "err3"){
alert("错误!该值不是数字!");
}
}
}
}
//检测浏览器及其版本
var browser = navigator.appName;
var b_version = navigator.appVersion;
var version = parseFloat(b_version);
document.write("浏览器名称:" + browser);
document.write("<br>");
document.write("浏览器版本" + b_version);
//检测浏览器的更多信息
document.write("<p>浏览器");
document.write(navigator.appName + "</P>");
document.write("<p>浏览器版本");
document.write(navigator.appVersion + "</p>");
document.write("<p>代码:");
document.write(navigator.appCode + "</p>");
document.write("<p>平台");
document.write(navigator.Platform + "</p>");
document.write("<p>Cookies启用");
document.write(navigator.CookieEnabled + "</p>");
document.write("<p>浏览器用户代理报头");
document.write(navigator.userAgent + "</p>");
//检测浏览器的全部信息
var x = navigator;
document.write("<br>");
document.write("minorVersion = " + appMinorVersion);
document.write("<br>");
document.write("CodeName = " + appCodeName);
document.write("<br>");
document.write("Name = " + appName);
document.write("<br>");
document.write("Version = " + appVersion);
document.write("<br>");
document.write("cookieEnabled =" + cookieEnabled);
document.write("<br>");
document.write("Online = " + onLine);
document.write("<br>");
document.write("CPUclass = "+ cpuClass);
document.write("<br>");
document.write("Platform = " + platForm);
document.write("<br>");
document.write("US = " + userAgent);
document.write("userLanguage = " + userLanguage);
document.write("<br>");
document.write("systemLanguage = " + systemLanguage);
document.write("<br>");
document.write("browserLanguage = " + browserLanguage);
//根据浏览器类型来提醒用户
function detectBrowser(){
var browser1 = navigator.appName;
var b_version1 = navigator.appVersion;
var version1 = parseFloat(b_version);
if(browser1 == "Netscape"|| browser == "Microsoft Internet Explorer"){
alert("你的浏览器很先进!");
}else{
alert("you should improve your navigator!");
}
}
</script>
</body>
</html>