ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# js 设置cookies > 需求: > 在a页面里需要传参数给b页面 > 设置token的过期刷新 ~~~ /** * 设置Cookies *@param key {String} cookies的键 * @param value {String} cookies的值 * @param expires {Number}过期时间 */ let setCookies = function (key, value, expires) { var cookieString=key+"="+encodeURIComponent(value); //判断是否设置过期时间 if(expires>0){ var date=new Date(); date.setTime(date.getTime+expires*3600*1000); cookieString=cookieString+"; expire="+date.toGMTString(); } document.cookie=cookieString; } /** * 获取cookies *@param key {String} cookies的键 */ let getCookies = function (key) { var strCookie=document.cookie; var arrCookie=strCookie.split("; "); for(var i=0;i<arrCookie.length;i++){ var arr=arrCookie[i].split("="); if(arr[0]==key)return arr[1]; } return ""; } /** * 删除指定cookie * @param key {String} cookies的名字 */ let deleteCookie = function (key) { var date=new Date(); date.setTime(date.getTime()-10000); document.cookie=key+"=v; expire="+date.toGMTString(); } export default { setCookies, getCookies, deleteCookie } ~~~