<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>cookie</title> </head> <body> 由于本地环境只有FF下支持cookie,请在FF下进行测试 <script> //document.cookie //格式 名字=值 //在js中等号“=”为赋值,如果对一个变量进行多次赋值,则会产生覆盖 //在cookie中不会 document.cookie = "user=blue"; document.cookie = "password=123"; var oDate = new Date(); //alert(oDate.getFullYear()+"-"+(oDate.getMonth()+1)+oDate.getDate());//IE下不能识别"2016-1-12"格式时间,但是能识别"2016/1/12" //oDate.setDate(oDate.getDate()+30)当前日期往后30天 oDate.setDate(oDate.getDate()+14);//14天有效期 document.cookie = "password=123;expires=oDate"; alert(document.cookie); function setCookie(name,value,iDay){ var oDate = new Date(); oDate.setDate(oDate.getDate()+iDay); document.cookie = name+"="+value+";expires="+oDate; } function getCookie(name){ var arr = document.cookie.split("; ");//注意空格不能少 for(var i=0;i<arr.length;i++){ var arr2 = arr[i].split("="); if(arr2[0] == name){ return arr2[1]; } } return ""; } function delCookie(name){ setCookie(name, 1,-1); } var login_proxy = {}; login_proxy.cookie = { // cookie操作 get : function (name){ var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)")); if(arr != null){ return unescape(arr[2]); } return ""; }, set : function (name, value, days,domain){ if (typeof days == "undefined"){ domain = domain?domain:'qq.com'; document.cookie = name + "=" + escape(value) + "; path=/; domain="+domain; }else{ var expdate = new Date(); var msecs = (typeof days === 'number') ? days * 24 * 60 * 60 * 1000 : 0; expdate.setTime(expdate.getTime() + msecs); document.cookie = name + "=" + escape(value) + "; path=/; domain=qq.com; expires=" + expdate.toUTCString(); } }, del : function (name){ var expdate = new Date(); expdate.setTime(expdate.getTime() - 1000); this.set(name, "", expdate); } }; </script> </body> </html>