;(function(window, undefined){ var CookieUtil = window.CookieUtil = window.CookieUtil || {}; var doc = window.document; // 读取名称为name的Cookie信息 CookieUtil.read = function(name){ // 暂无Cookie if(doc.cookie.length <= 0){ return null; } // 获取所有cookie信息 var cookies = doc.cookie.split(';'); var size = cookies.length, i, c; // 遍历查找要读取的cookie信息 for(i = 0; i < size; i++){ c = cookies[i].split('='); // 找到cookie,需要trim,写入的cookie自动添加了空格 if($.trim(c[0]) == name){ return unescape(c[1]); } } return null; }; // 写入名称为name,值为value的Cookie信息,过期时间为timeout秒 CookieUtil.write = function(name, value, timeout){ var flg = timeout === undefined; var exp = flg ? 0 : timeout; // 获取过期时间 var expires = new Date(); expires.setSeconds(expires.getSeconds() + exp); // 写入Cookie信息 doc.cookie = name + '=' + escape(value) + (flg ? '' : (';expires=' + expires.toGMTString())); }; // 删除名称为name的Cookie信息 CookieUtil.remove = function(name){ CookieUtil.write(name, null, -1); };})(window);
版权声明:本文为博主原创文章,未经博主允许不得转载。