首页 > 代码库 > javascript设置和获取cookie值
javascript设置和获取cookie值
//设置cookiefunction setCookie(name,value,expires,path,domain,secure){ var cookieName = encodeURIComponent(name) + ‘=‘ + encodeURIComponent(value); if(expires instanceof Date){ cookieName += ‘;expires=‘ + expires; } if(path){ cookieName += ‘;path=‘ + path; } if(domain){ cookieName += ‘;domain=‘ + domain; } if(secure){ cookieName += ‘;secure=‘ + true; } document.cookie = cookieName;}//过期时间function setCookieDate(day){ var date = null; if(typeof day == ‘number‘ && day > 0){ date = new Date(); date.setDate(date.getDate() + day); }else{ throw new Error(‘您传递的天数不合法,必须是数字且大于0‘); } return date;}setCookie(‘user‘,‘小飞‘,setCookieDate(7));setCookie(‘url‘,‘baidu.com‘,setCookieDate(7));setCookie(‘email‘,‘1253185@qq.com‘,setCookieDate(7));//获取cookiefunction getCookie(name){ var cookieName = encodeURIComponent(name) + ‘=‘; var cookieStart = document.cookie.indexOf(cookieName); var cookieValue = http://www.mamicode.com/null; if(cookieStart>-1){ var cookieEnd = document.cookie.indexOf(‘;‘,cookieStart); if(cookieEnd == -1){ cookieEnd = document.cookie.length; } cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length,cookieEnd)); } return cookieValue;}alert(getCookie(‘email‘));
javascript设置和获取cookie值
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。