JS Javascript 操作Cookie

--未测试 留着参考

中文时可以用 unescape、escape 或是 encodeURI、decodeURI 进行转换

一:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>JS操作cookie.html</title>
	
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <script type="text/javascript" src="JSCookies.js"></script>
`   <script type="text/javascript">
		//用法:   
		//一、设置cookie   
		var cookie = new Cookies(); 
		function set() {  
			//普通设置   
			cookie.set("key1", "哈哈3");   
			
			//过期时间为一年   
			var d = new Date();   
			d.setFullYear(d.getFullYear() + 1);   
			cookie.set("key2", "val2", d);   
			  
			//设置域及路径,带过期时间   
			cookie.set("key3", "val3", d, "casett", "/");   
			  
			//设置带子键的cookie,子键分别是k1,k2,k3   
			cookie.set("key4", "k1=哈哈是&k2=2&k3=3");   
		};
		  
		//二、读取cookie   
		//简单获取   
		function get() {
			alert(cookie.get("key1"));   
			alert(cookie.get("key2"));   
			alert(cookie.get("key3"));   
			alert(cookie.get("key4"));   
			//获取key4的子键k1值   
			alert(cookie.getChild("key4","k1"));   
			alert(cookie.getChild("key4","k2"));   
		}
		
		//三、删除   
		function remove() {
			cookie.remove("key1");   
			cookie.remove("key2");   
			cookie.remove("key3");   
			cookie.remove("key4");
		} 
    </script>
  </head>
  
  <body>
  	<input type="button" value="set" οnclick="set()"/>
  	<input type="button" value="get" οnclick="get()"/>
  	<input type="button" value="clear" οnclick="remove()"/>
  </body>
</html>

二:JSCookies.js

String.prototype.Trim = function() {
	return this.replace(/^/s+/g, "").replace(//s+$/g, "");
}

function Cookies() {
	this.get = function(key) {
		var cookie = document.cookie;
		var cookieArray = cookie.split(';');
		var val = "";
		for (var i = 0; i < cookieArray.length; i++) {
			if (cookieArray[i].Trim().substr(0, key.length) == key) {
				val = cookieArray[i].Trim().substr(key.length + 1);
				break;
			}
		}
		return unescape(val);
	};
	this.getChild = function(key, childKey) {
		var child = this.get(key);
		var childs = child.split('&');
		var val = "";

		for (var i = 0; i < childs.length; i++) {
			if (childs[i].Trim().substr(0, childKey.length) == childKey) {
				val = childs[i].Trim().substr(childKey.length + 1);
				break;
			}
		}
		return val;
	};
	this.set = function(key, value) {
		var cookie = "";
		if (!!key && !!value)
			cookie += key + "=" + escape(value) + ";";
		if (!!arguments[2])
			cookie += "expires=" + arguments[2].toGMTString() + ";";
		if (!!arguments[3])
			cookie += "domain=" + arguments[3] + ";";
		if (!!arguments[4])
			cookie += "path=" + arguments[4] + ";";
		document.cookie = cookie;
	};
	this.remove = function(key) {
		var date = new Date();
		date.setFullYear(date.getFullYear() - 1);
		var cookie = " " + key + "=;expires=" + date + ";"
		document.cookie = cookie;
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值