js 防止cookie 重复覆盖
function addCookie(name, value, expiresHours) { var cookieString = name + "=" + escape(value); //判断是否设置过期时间,0代表关闭浏览器时失效 if (expiresHours > 0) { var date = new Date(); date.setTime(date.getTime() + expiresHours * 1000); cookieString = cookieString + ";expires=" + date.toGMTString() + ";path=" + window.location.pathname; console.log(cookieString); } document.cookie = cookieString; }
";path=" + window.location.pathname;
多个网页设置相同的cookie时候不会覆盖,英文在不同的路径下。