cookie.js

来自「自己整理的H.264相关资料」· JavaScript 代码 · 共 65 行

JS
65
字号
// 167-279-1
/* @some operations on cookie,include set,get and delete
 * if the blowser doesn't support cookie,it will throw a exception.
 * toCheckIsSupportCookie,检验是否支持cookie,默认不检验
 */
function Cookie(toCheckIsSupportCookie){
	if(toCheckIsSupportCookie==true && !this.tstCookie()){
		throw new Error("this blowser doesn't support cookie");
	}
}
/* @set cookie
 * name,cookie's name;value,cookie's value;these two arguments are necessary.
 * expires,cookie's expires time;
 */
Cookie.prototype.setCookie=function(name,value,expires,path,domain,secure){
	if(arguments.length>=2){
		var cookie=name+"="+encodeURIComponent(value);
		if(expires){
			cookie+=";expires="+expires.toGMTString();
		}
		if(path){
			cookie+=";path="+path;
		}
		if(domain){
			cookie+=";domain="+domain;
		}
		if(secure){
			cookie+=";secure";
		}
		document.cookie=cookie;
	}else{
		throw new Error("the arguments name and value are necessary");
	}
}
/* @get cookie
 * name,cookie's name.
 */
Cookie.prototype.getCookie=function(name){
	var sRE="(?:; )?"+name+"=([^;]*);?";
	var oRE=new RegExp(sRE);
	if(oRE.test(document.cookie)){
		return decodeURIComponent(RegExp["$1"]);
	}else{
		return null;
	}
}
/* @delete cookie
 * name,cookie's name,it's necessary.
 * if set cookie include path and domain arguments,
 * delete cookie named name,also must include path and domain the same as setted value.
 */
Cookie.prototype.delCookie=function(name,path,domain){
	this.setCookie(name,"",new Date(0),path,domain);
}
/* @test whether the blowser supports cookie or not
 */
Cookie.prototype.tstCookie=function(){
	this.setCookie("testCookie","testCookie");
	if("testCookie"==this.getCookie("testCookie")){
		this.delCookie("testCookie");
		return true;
	}else{
		return false;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?