javascript的cookie相关函数


JavaScript #cookie #函数2012-05-09 16:36
/** ********************************************* */
/* Cookie yige.org */
/** ********************************************* */
// 新建cookie。
function setCookie(name, value, hours, path) {
	var name = encodeURIComponent(name);
	var value = encodeURIComponent(value);
	var expires = new Date();
	expires.setTime(expires.getTime() + hours * 3600000);
	path = path == "" ? "" : ";path=" + path;
	_expires = (typeof hours) == "string" ? "" : ";expires="
			+ expires.toUTCString();
	document.cookie = name + "=" + value + _expires + path;
}
// 获取cookie值
function getCookieValue(name) {
	var name = encodeURIComponent(name);
	var allcookies = document.cookie;
	name += "=";
	var pos = allcookies.indexOf(name);
	if (pos != -1) {
		var start = pos + name.length;
		var end = allcookies.indexOf(";", start);
		if (end == -1)
			end = allcookies.length;
		var value = allcookies.substring(start, end);
		return decodeURIComponent(value);
	} else
		return "";
}
// 删除cookie
function deleteCookie(name, path) {
	var name = encodeURIComponent(name);
	var expires = new Date(0);
	path = path == "" ? "" : ";path=" + path;
	document.cookie = name + "=" + ";expires=" + expires.toUTCString() + path;
}

相关文章

粤ICP备11097351号-1