欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

javascript基础学习自我总结

程序员文章站 2022-06-30 20:48:20
...

1.JavaScript循环

for (var i=0; i<=10;i++){
    if (i==3){
        continue;//当i=3时终端循环,继续下面
    }
    document.write("数字是 " + i)
    document.write("<br />")
}
var x;
var mycars = new Array()
mycars[0] = "宝马"
mycars[1] = "奔驰"
mycars[2] = "宾利"

for (x in mycars){
    document.write(mycars[x] + "<br />")
}

2.try ... catch语句

var txt="";
function message(){
try
   {
   adddlert("Welcome guest!"); //不存在此方法,会报异常
   }
catch(err) {
   txt="本页中存在错误。\n\n";
   txt+="错误描述:" + err.description + "\n\n";
   txt+="点击“确定”继续。\n\n";
   console.log(txt);
   }
}

3.throws语句

var x=prompt("请输入 0 至 10 之间的数:","")
try{ 
    if(x>10)   throw "Err1" ;
    else if(x<0) throw "Err2";
    else if(isNaN(x)) throw "Err3";
} catch(er){
    if(er=="Err1") alert("错误!该值太大!");
    if(er == "Err2")  alert("错误!该值太小!") ;
    if(er == "Err3")   alert("错误!该值不是数字!") ;
}

4.onerror事件

onerror=handleErr;  
var txt="";

function handleErr(msg,url,l){
    txt="本页中存在错误。\n\n";
    txt+="错误:" + msg + "\n";
    txt+="URL: " + url + "\n";
    txt+="行:" + l + "\n\n";
    txt+="点击“确定”继续。\n\n";
    alert(txt)
    return true
}

function message(){
    adddlert("Welcome guest!") //没有此方法,就会触发 onerror事件
}

5.获取浏览器版本

var browser=navigator.appName;
var b_version=navigator.appVersion;
var version=parseFloat(b_version);
document.write("浏览器名称:"+ browser);
document.write("<br />");
document.write("浏览器版本:"+ version);

6.创建一个cookie

function getCookie(c_name) {
	if(document.cookie.length > 0) {
		c_start = document.cookie.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.cookie.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) +   //escape编码函数   unescape解码函数
		((expiredays == null) ? "" : "; expires=" + exdate.toGMTString())
}

function checkCookie() {
	username = getCookie('username')
	if(username != null && username != "") {
		alert('Welcome again ' + username + '!');
	} else {
		username = prompt('Please enter your name:', "")
		if(username != null && username != "") {
			setCookie('username', username, 365);
		}
	}
}

7.定时器函数

var c = 0
var t
//启用定时器函数
function timedCount() {
	document.getElementById('txt').value = c
	c = c + 1
	t = setTimeout("timedCount()", 1000)
}
//停止定时器函数
function stopCount() {
	c = 0;
	setTimeout("document.getElementById('txt').value=0", 0);
	clearTimeout(t);
}
function startTime() {  //使用定时器做时钟
	var today = new Date()
	var h = today.getHours()
	var m = today.getMinutes()
	var s = today.getSeconds()
	// add a zero in front of numbers<10
	m = checkTime(m)
	s = checkTime(s)
	document.getElementById('txt').innerHTML = h + ":" + m + ":" + s
	t = setTimeout('startTime()', 500)
}

function checkTime(i) {
	if(i < 10) {
		i = "0" + i
	}
	return i
}

8.创建对象示例

personObj=new Object()
personObj.firstname="John";
personObj.lastname="Adams";
personObj.age=35;
personObj.eyecolor="black";

document.write(personObj.firstname + " 的年龄是 " + personObj.age + " 岁。");

9.对象模板

function person(firstname, lastname, age, eyecolor) {
	this.firstname = firstname;
	this.lastname = lastname;
	this.age = age;
	this.eyecolor = eyecolor;
}

myFather = new person("John", "Adams", 35, "black");

document.write(myFather.firstname + " 的年龄是 " + myFather.age + " 岁。");

10.数组函数

var mycars = new Array()
mycars[0] = "Saab";
mycars[1] = "Volvo";
mycars[2] = "BMW";

for(i = 0; i < mycars.length; i++) {
	document.write(mycars[i] + "<br />");
}

11.获取url中的参数

/**
 * 截取路径上字符串的方法.1
 * @param {Object} name
 */
function getQueryString(name) {
	var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
	var r = window.location.search.substr(1).match(reg);
	if(r != null) {
		return unescape(r[2]);
	}
	return null;
}
/**
 * split拆分法
 */
function getRequest() {
	var url = window.location.search;
	var theRequest = new Object();
	if(url.indexOf("?") != -1) {
		var str = url.substr(1);
		strs = str.split("&");
		for(var i = 0; i < strs.length; i++) {
			theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
		}
	}
	return theRequest;
}
/**
 * 方法三
 * @param {Object} name
 */
function GetQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
	var context = "";
	if(r != null)
		context = r[2];
	reg = null;
	r = null;
	return context == null || context == "" || context == "undefined" ? "" : context;
}
/**
 * 截取路径上?后面的参数
 */
function getRequest() {
	var url = location.search;
	if(url.indexOf('?') != -1) {
		var str = url.substr(1);
		var strs = str.split("=");
		return strs[1];
	}

}