js操作cookie的疑惑

shuiping567541 2012-01-06 10:39:51
function Cookie(name){
this.$name = name;
var allcookies = document.cookie;
if(allcookies == "")return;
var cookies = allcookies.split(";");
var cookie = null;
for(var i = 0; i < cookies.length; i++){
// Does this cookie string begin with the name we want?
if (cookies[i].substring(0, name.length+1) == (name + "=")){
cookie = cookies[i];
break;
}
}
if (cookie == null) return;
var cookieval = cookie.substring(name.length+1);
var a = cookieval.split("&");
for(var i=0; i < a.length; i++){
a[i] = a[i].split(":");
}
for(var i=0; i < a.length; i++){
this[a[i][0]] = decodeURIComponent(a[i][1]);
}
}
/**
* This function is the store() method of the Cookie object.
*
* Arguments:
*
* daysToLive: the lifetime of the cookie, in days. If you set this
* to zero, the cookie will be deleted. If you set it to null, or
* omit this argument, the cookie will be a session cookie and will
* not be retained when the browser exits. This argument is used to
* set the max-age attribute of the cookie.
* path: the value of the path attribute of the cookie
* domain: the value of the domain attribute of the cookie
* secure: if true, the secure attribute of the cookie will be set
*/
Cookie.prototype.store = function(daysToLive, path, domain, secure){
var cookieval = "";
for(var prop in this){
if ((prop.charAt(0) == "$") || ((typeof this[prop]) == "function"))
continue;
if (cookieval != "") cookieval += "&";
cookieval += prop + ":" + encodeURIComponent(this[prop]);
}
var cookie = this.$name + "=" + cookieval;
if (daysToLive || daysToLive == 0){
cookie += "; max-age=" + (daysToLive*24*60*60);
}
if (path) cookie += "; path=" + path;
if (domain) cookie += "; domain=" + domain;
if (secure) cookie += "; secure";

document.cookie = cookie;
};

/**
* This function is the remove() method of the Cookie object; it deletes the
* properties of the object and removes the cookie from the browser's
* local store.
*
* The arguments to this function are all optional, but to remove a cookie
* you must pass the same value you passed to store().
*/
Cookie.prototype.remove = function(path,domain, secure){
for(var prop in this){
if (prop.charAt(0) != "$" && typeof this[prop] != "function")
delete this[prop];
}
this.store(0,path,domain,secure);
};

/**
* This static method attempts to determine whether cookies are enabled.
* It returns true if they appear to be enabled and false otherwise.
* A return value of true does not guarantee that cookies actually persist .
* Nonpersistent session cookies may still work even if this method
* return false.
*/
Cookie.enabled = function(){
if (navigator.cookieEnabled != undefined) return navigator.cookieEnabled;
if (Cookie.enabled.cache != undefined) return Cookie.enabled.cache;
document.cookie = "testcookie=test; max-age=10000";
var cookies = document.cookie;
if (cookies.indexOf("testcookie=test") == -1){
return Cookie.enable.cache = false;
}
else{
document.cookie = "testcookie=test; max-age=0";
return Cookie.enable.cache = true;
}
};


上面是从js权威指南上摘抄下来的代码

按照代码 , 我是这样操作的cookie

var cookie=new Cookie('docman');
cookie.close=1;
cookie.store(1,"/");

这样的话 我只要在同意域名下,就能读取到这个cookie,但是 我只要离开设置cookie的页面就读取不到了

求解 , 我用的是火狐和google的浏览器 。。。。。
可能是我水平实在有限
望高人指教
...全文
145 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
jayrao5566 2012-01-07
  • 打赏
  • 举报
回复

设置cookie, 'path=/'
丁码农 2012-01-06
  • 打赏
  • 举报
回复
cookie跨域是不行的。推荐你使用jquery cookie插件。

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧