JavaScript操作Cookie着实麻烦,这也是很多Web开发者比较头痛的一块。JavaScript Cookie写起来可能不是很难,但是要写出漂亮的代码就不那么简单了。以前写过几个函数来方便JS操作Cookie(JavaScript Cookie操作我不怕了),今天再献上一个JavaScrip Cookie操作的封装类。
String.prototype.format = function() {
var s = this;
for (var i = 0, j = arguments.length; i < j; i++)
s = s.replace(“{” + (i) + “}”, arguments[i]);
return(s);
}
var Cookie = {
Set : function () {
var name = arguments[0], value = escape(arguments[1]),
days = (arguments.length > 2) ? arguments[2] : 365,
path = (arguments.length > 3) ? arguments[3] : “/”;
with(new Date()) {
setDate(getDate() + days);
days = toUTCString();
}
document.cookie = “{0}={1};expires={2};path={3}”.format(name, value, days, path);
},Get : function () {
var returnValue = document.cookie.match(new RegExp(“[\b\^;]?” + arguments[0] + “=([^;]*)(?=;|\b|$)”,”i”));
return returnValue ? unescape(returnValue[1]) : returnValue;
},Delete : function () {
var name = arguments[0];
document.cookie = name + “=1 ; expires=Fri, 31 Dec 1900 23:59:59 GMT;”;
}
}
<script type=”text/javascript”>
Cookie.Set(‘MyCookie’, ‘Cookie值’);
Cookie.Get(‘MyCookie’);
Cookie.Delete(‘MyCookie’);
</script>
PHPLAMP博客是专注于网站建设,搜索引擎研究,网站推广,网站优化的IT博客。
您好,请问您这个代码使用的是什么字体?我非常喜欢。
Comic Sans MS
我一直是用jquery.cookie来使用cookie的,原生JS写的
源码拿走,闪人。。。