javaScript 操作COOKIE執行個體代碼分享

來源:互聯網
上載者:User

相對於javascript,通過後端的php來操作cookie是更容易簡單的。所以由於以前很少通過JS去操作cookie,在使用javascript操作cookie時遇到了一些彎路,也分享出來,免得大家跟我犯同樣的錯誤。

第一點,就是我知道document.cookie返回所有cookie組成的字串,所以在設定cookie時,我想當然的以為是將新cookie拼接到該字串上,再賦值給document.cookie就行了。

經過測試,完全不是這樣,document.cookie=””; 是添加或更新新的cookie,比如document.cookie=”myck=yes;”則添加了個名字為myck的cookie;如果要同時添加多個cookie,document.cookie=”key1=1;key2=2;“ 就行了。如果要加上到期時間,儲存域等資訊,直接後面加上相關參數即可。比如:document.cookie=’myck=yes; expires=expire_time;domain=domain’。

該操作完全不影響已有的非同名cookie。

第二點,設定cookie的到期時間時,我錯誤的以為是設定個時間戳記就行了,結果測來測去,發現沒有效,cookie有效期間總是session周期。後來才發現要用標準字串時間格式,類似”Mon Jul 23 2012 20:08:10 GMT+0800 GMT“。如果用1343045321299這種,則是無效的。


cookie.js檔案

 代碼如下 複製代碼

var COOKIE=(function(){

var getDateString=function(offset){

var date=new Date();

date.setTime(+date+offset*1000);

return date.toGMTString();

},

getCookies=function(){

var cookie=document.cookie||'',

subs=cookie.split(/;s?/),

_subs,cks={};

for(var i=0;i<subs.length,subs[i];i++){

_subs=subs[i].split('=')

cks[unescape(_subs[0])]=unescape(_subs.slice(1).join('='));

}

return cks;

}

 


return {

refresh:function(){

this.cookies=getCookies();

return this;

},

has:function(key){

return this.cookies[key]!=null;

},

get:function(key){

return this.cookies[key];

},

set:function(key,value,expire,path,domain,secure){

var myck=escape(key)+'='+escape(value==null?'':value);

if(!isNaN(expire=parseFloat(expire)))

myck+=';expires='+getDateString(expire);

if(path)myck+=';path='+path;

if(domain&&domain!=location.hostname)myck+=';domain='+domain;

if(secure)myck+=';secure';

document.cookie=myck;

return this.refresh().has(key);

},

remove:function(key,path,domain){

var paths=[],

domains=[],

arr,self=this;

if(path){

paths=[path];

}else{

arr=location.pathname.match(/.*?/|.+$/g);

this.each(arr,function(i){

var a;

paths.push(a=arr.slice(0,i+1).join(''));

if(/[^/]+/$/.test(a)){

paths.push(a.slice(0,-1));

}

if(/[^/]$/.test(a)){

paths.push(a+'/');

}

});

}

 


if(domain){

domains=[domain];

}else{

arr=location.hostname.split('.');

this.each(arr,function(i){

domains.push(arr.slice(-i).join('.'));

});

domains.push('.'+domains[0]);

}

 


this.each(paths,function(){

var path=this+'';

self.each(domains,function(){

self.set(key,'',-1000,path,this+'');

});

});

 


return !!path||!!domain||!this.has(key);

},

clear:function(path,domain){

for(var key in this.cookies){

this.remove(key,path,domain);

}

return !!path||!!domain||function(){

for(var key in this.cookies){

return false;

}

return true;

}.call(this);

},

each:function(arr,func){

var i=0,j=arr.length;

for(;i<j;i++){

if(func.call(arr[i],i)===false){

break;

}

}

}

}.refresh();

})();

// 首先在頁面中引入COOKIE.js

 代碼如下 複製代碼

//調用
COOKIE.has(name); //檢測是否有名字為name的cookie
COOKIE.set(key,value,expire,path,domain); //設定一個新cookie,true表示設定成功,false表示設定失敗
COOKIE.remove(name,path,domain); //刪除名為name的cookie,true表示刪除成功,false表示刪除失敗
COOKIE.get(name); //擷取名為name的cookie的值
COOKIE.clear(path,domain); //清除所有cookie

//注意:remove和clear方法,如果不設定path或domain,將會刪除所有根域、子域、各個深度路徑下的cookie。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.