/** * Cookie操作 * 调用方式: * Cookie.add(name, value, expiresHours) * add 添加 * get 获取 * delete 删除 */ var Cookie = { // 添加cookie // name 名 // value 值 // expiresHours 过期时间,单位小时 add : function(name, value, expiresHours){ // escape() 函数对特殊符号转义,比如 ‘=’ 和 ‘;’ ,应为在cookie它们有特殊含义 var strCookie = name + '=' + escape(value); if(expiresHours > 0){ var date = new Date(); date.setTime(date.getTime() + expiresHours * 3600 * 1000); strCookie = strCookie + '; expires=' + date.toGMTString(); } document.cookie = strCookie; }, // 根据名获取cookie // name 名 get : function(name){ var strCookie = document.cookie; var arrCookie = strCookie.split('; '); for (var i = 0; i < arrCookie.length; i++) { var cookie = arrCookie[i].split('='); if(cookie[0] === name){ // unescape() 对特殊字符串解码 return unescape(cookie[1]); } }; return null; }, // 根据名删除cookie // name 名 delete : function(name){ var date = new Date(); date.setTime(date.getTime() - 10000); document.cookie = name + '=destroy; expires=' + date.toGMTString(); } } /** * Ajax封装 * post 请求 */ var Ajax = { post : function (url, param, success){ $.post(url, param, function(data, stat){ if(stat != 'success'){ alert('网络连接失败...'); return; } var data = JSON.parse(data); if(data.success === 'true'){ success(data.data); }else{ notifier.failed(data.msg); } }); } }