Home >php教程 >php手册 >cookie的更新失败

cookie的更新失败

WBOY
WBOYOriginal
2016-06-07 11:35:361805browse

问题描述:公司网站用cookie,存取用户的省市区。bug:切换省市区时,火狐下失败,chrome成功。查看cookie,有2个ipLoc-djd,域分别为.a 和 .b。考虑可能是这个问题。
解决办法:
//保存
function setCookie(name, value) {
var Days = 30;
var exp = new Date();
exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + ";path=/";
}
//取cookie
function getCookie(name) {
var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
if (arr = document.cookie.match(reg)) {
return unescape(arr[2])
} else {
return null
}
}
上面是一般的方法了,cookie域取默认值.b。而项目需要制定cookie域.a,于是百度查到可以指定cookie域
修改后
function setCookie(name, value) {
var Days = 30;
var exp = new Date();
exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + ";path=/"+";domain=.a";
}
清除cookie,刷新页面,问题完美解决。

AD:真正免费,域名+虚机+企业邮箱=0元

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn