JavaScript 操作 Cookie
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
//目前b/s程序開發(fā)及應(yīng)用過程中,cookie應(yīng)用相當(dāng)頻繁 //如:用戶登陸 ,保存用戶偏好設(shè)置 等等 //下面的類為方面操作cookie提供了幫助 //date:2008/4/24 by danxinju var Cookie = new Object(); //定義Cookie對象 Cookie.setCookie = function(name/*cookie鍵*/, value/*鍵值*/, option/*選項(xiàng):過期設(shè)置*/){ //設(shè)置cookie var str = name +'='+ escape(value); if(option){ if(option.expireHours){ var d=new Date(); d.setTime(d.getTime()+option.expireHours*3600*1000); str += '; expires='+d.toGMTString(); } if(option.path) str += '; path='+option.path; if(option.domain) str += '; domain='+option.domain; if(option.secure) str += '; true'; } document.cookie = str; }; Cookie.getCookie = function(name/*cookie鍵*/){ //獲取一個(gè)cookie值 var arr = document.cookie.split('; '); if(arr.length == 0) return ''; for(var i=0; i if(tmp[0] == name) return unescape(tmp[1]); } return ''; }; Cookie.delCookie = function(name/*cookie鍵*/){ //刪除一個(gè)cookie this.setCookie(name,'',{expireHours:-1}); }; Cookie.length = function(){ //獲取cookie個(gè)數(shù) return document.cookie.split('; ').length; }; /**How to use it Cookie.setCookie('own','this is cookie test!'); alert(Cookie.getCookie('own')); Cookie.delCookie('own'); alert(Cookie.getCookie('own')); **/ 該文章在 2010/8/13 19:10:53 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |