<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function Cookie(document, name, hours, path, domain, secure) {
this.$document = document;
this.$name = name;
if (hours)
this.$expiration = new Date((new Date()).getTime() + hours * 3600000);
else this.$expiration = null;
if (path) this.$path = path; else this.$path = null;
if (domain) this.$domain = domain; else this.$domain = null;
if (secure) this.$secure = true; else this.$secure = false;
}
Cookie.prototype.store = function() {
var cookieval = "";
for (var prop in this) {
// Ignore properties with names that begin with '$' and also methods.
if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function'))
continue;
if (cookieval != "") cookieval += '&';
cookieval += prop + ':' + escape(this[prop]);
}
var cookie = this.$name + '=' + cookieval;
if (this.$expiration)
cookie += '; expires=' + this.$expiration.toGMTString();
if (this.$path) cookie += '; path=' + this.$path;
if (this.$domain) cookie += '; domain=' + this.$domain;
if (this.$secure) cookie += '; secure'; // Now store the cookie by setting the magic Document.cookie property.
this.$document.cookie = cookie;
}
Cookie.prototype.load = function() {
var allcookies = this.$document.cookie;
if (allcookies == "") return false; var start = allcookies.indexOf(this.$name + '=');
if (start == -1) return false; // Cookie not defined for this page.
start += this.$name.length + 1; // Skip name and equals sign.
var end = allcookies.indexOf(';', start);
if (end == -1) end = allcookies.length;
var cookieval = allcookies.substring(start, end); var a = cookieval.split('&'); // Break it into array of name/value pairs.
for (var i = 0; i < a.length; i++) // Break each pair into an array.
a[i] = a[i].split(':'); for (var i = 0; i < a.length; i++) {
this[a[i][0]] = unescape(a[i][1]);
} return true;
} // This function is the remove() method of the Cookie object.
Cookie.prototype.remove = function() {
var cookie;
cookie = this.$name + '=';
if (this.$path) cookie += '; path=' + this.$path;
if (this.$domain) cookie += '; domain=' + this.$domain;
cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT'; this.$document.cookie = cookie;
} var visitordata = new Cookie(document, "name_color_count_state", 240); if (!visitordata.load() || !visitordata.name || !visitordata.color) {
visitordata.name = prompt("What is your name:", "");
visitordata.color = prompt("What is your favorite color:", "");
} if (visitordata.visits == null) visitordata.visits = 0;
visitordata.visits++; visitordata.store(); document.write('<font size="7" color="' + visitordata.color + '">' +
'Welcome, ' + visitordata.name + '!' +
'</font>' +
'<p>You have visited ' + visitordata.visits + ' times.');
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="Forget My Name" onclick="visitordata.remove();">
</div>
</form>
</body>
</html>username=jsh; pwd=jsh; button1=yyy; button2=jjj; button3=jjj; name_color_count_state=name:jsh&color:red&visits:1
下面这个就是他的cookie,与前面的比就是将cookie的值 串成 key:value&key:value的形式。这样做有什么用吗?
这与我直接设置 某一个listproductcookie 的值 为: productlistcookie=name:jsh&pwd:jsh&key:value.这样不是一样吗?
还有这里用“:”会不会产生错误的?不过,我测试过是不会的。
请问这样操作cookie好吗?
还是用jquery 插件好?
<head runat="server">
<title></title>
<script type="text/javascript">
function Cookie(document, name, hours, path, domain, secure) {
this.$document = document;
this.$name = name;
if (hours)
this.$expiration = new Date((new Date()).getTime() + hours * 3600000);
else this.$expiration = null;
if (path) this.$path = path; else this.$path = null;
if (domain) this.$domain = domain; else this.$domain = null;
if (secure) this.$secure = true; else this.$secure = false;
}
Cookie.prototype.store = function() {
var cookieval = "";
for (var prop in this) {
// Ignore properties with names that begin with '$' and also methods.
if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function'))
continue;
if (cookieval != "") cookieval += '&';
cookieval += prop + ':' + escape(this[prop]);
}
var cookie = this.$name + '=' + cookieval;
if (this.$expiration)
cookie += '; expires=' + this.$expiration.toGMTString();
if (this.$path) cookie += '; path=' + this.$path;
if (this.$domain) cookie += '; domain=' + this.$domain;
if (this.$secure) cookie += '; secure'; // Now store the cookie by setting the magic Document.cookie property.
this.$document.cookie = cookie;
}
Cookie.prototype.load = function() {
var allcookies = this.$document.cookie;
if (allcookies == "") return false; var start = allcookies.indexOf(this.$name + '=');
if (start == -1) return false; // Cookie not defined for this page.
start += this.$name.length + 1; // Skip name and equals sign.
var end = allcookies.indexOf(';', start);
if (end == -1) end = allcookies.length;
var cookieval = allcookies.substring(start, end); var a = cookieval.split('&'); // Break it into array of name/value pairs.
for (var i = 0; i < a.length; i++) // Break each pair into an array.
a[i] = a[i].split(':'); for (var i = 0; i < a.length; i++) {
this[a[i][0]] = unescape(a[i][1]);
} return true;
} // This function is the remove() method of the Cookie object.
Cookie.prototype.remove = function() {
var cookie;
cookie = this.$name + '=';
if (this.$path) cookie += '; path=' + this.$path;
if (this.$domain) cookie += '; domain=' + this.$domain;
cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT'; this.$document.cookie = cookie;
} var visitordata = new Cookie(document, "name_color_count_state", 240); if (!visitordata.load() || !visitordata.name || !visitordata.color) {
visitordata.name = prompt("What is your name:", "");
visitordata.color = prompt("What is your favorite color:", "");
} if (visitordata.visits == null) visitordata.visits = 0;
visitordata.visits++; visitordata.store(); document.write('<font size="7" color="' + visitordata.color + '">' +
'Welcome, ' + visitordata.name + '!' +
'</font>' +
'<p>You have visited ' + visitordata.visits + ' times.');
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="Forget My Name" onclick="visitordata.remove();">
</div>
</form>
</body>
</html>username=jsh; pwd=jsh; button1=yyy; button2=jjj; button3=jjj; name_color_count_state=name:jsh&color:red&visits:1
下面这个就是他的cookie,与前面的比就是将cookie的值 串成 key:value&key:value的形式。这样做有什么用吗?
这与我直接设置 某一个listproductcookie 的值 为: productlistcookie=name:jsh&pwd:jsh&key:value.这样不是一样吗?
还有这里用“:”会不会产生错误的?不过,我测试过是不会的。
请问这样操作cookie好吗?
还是用jquery 插件好?
解决方案 »
- jquery 请问我要append一段很长的html代码怎样换行?
- JS弹窗效果
- JavaScript实例化错误
- 请问怎样实现将鼠标移到下拉框的某项上时,就显示该下拉框项的提示信息?
- 请求高手:Javascript与ssl的问题
- 弱弱的问题,关于document.write换行
- 请问什么版本的哪些浏览器支持designMode和contentEditable属性?
- 大家帮忙看看这个怎么才能执行?document.yhtz.字符串(传过来的).options[]...马上给分!
- showModalDialog的窗口隐藏关闭按钮
- 如何传递数据?
- 请问openlayer怎么删除标注?
- jquery如何判断当前对象是什么咚咚?
写成 productlistcookie=name:jsh&pwd:jsh&key:value
取的时候适当拆分
而且他这个要先加载然后再存储的
# function addCookie(name,value,expireHours){
# var CookieStr=name+"="+escape(value);
# if(expireHours>0){
# var date=new Date();
# date.setTime(date.getTime()+expireHours*3600*1000);
# CookieStr=CookieStr+";expire="+date.toGMTString();
# }
# document.cookie=CookieStr;
# }
# function getCookie(name){
# var strCookie=document.cookie;
# var arrCookie=strCookie.split(";");
# for(var i=0;i<arrCookie.length;i++){
# var arr=arrCookie[i].split("=");
# if(arr[0]==name){
# return arr[1];
# }
# return "";
# }
# }
jquery当然是比较简单些,兼容性也好
题外话啦。。
谢谢大家
我觉得用jQuery方便多了..而且兼容要很多的。。
都可有优点吧,cookie本来长度都有限制,加在一起,更容易超出长度限制了
另一方面cookie 中key的个数也有限制,这样又有好处
more detail
sky 有给了我另一个问题的答案啦。。谢谢