/ 前端
分类:前端来源:站内 最近更新:2020-09-18 17:44:28浏览:1523留言:0
设置cookies
function setCookies(name, value, expires, domain,path, secure) { var cookie = encodeURIComponent(name) + "=" + encodeURIComponent(value); if (expires instanceof Date) { cookie += "; expires=" + expires.toGMTString(); } else { var date = new Date(); date.setTime(date.getTime() + expires * 24 * 3600 * 1000); cookie += "; expires=" + date.toGMTString(); } if (path) { cookie += "; path=" + path; } if (domain) { cookie += "; domain=" + domain; } if (secure) { cookie += "; " + secure; } document.cookie = cookie; }
获得cookies
function(name) { var cookieName = encodeURIComponent(name); /*正则表达式获取cookie*/ var restr = "(^| )" + cookieName + "=([^;]*)(;|$)"; var reg = new RegExp(restr); var cookieValue = document.cookie.match(reg); if (cookieValue) { return cookieValue[2]; } else { return null; } }