CookieHelper JS封装Cookie 存取方法

微信的一些页面会去获取授权,然后在回调到页面,但是这样的话通过url传递的参数有可能丢失掉,我采用存储cookie的方式来传值

建一个CookieHelper.js文件

function CookieHelper() {

}
//删除cookie
CookieHelper.prototype.deleteCookie = function (name) {
var date = new Date();
date.setTime(date.getTime() - 10000);
document.cookie = name + "=v; expires=" + date.toGMTString() + ";path=/";
} //设置cookie
CookieHelper.prototype.setCookie = function (name, value) {
//设置之前先删除
this.deleteCookie(name);
//获取当前时间
var date = new Date();
var expiresDays = 10;
//将date设置为10天以后的时间
date.setTime(date.getTime() + expiresDays * 24 * 3600 * 1000);
//cookie设置为10天后过期
document.cookie = name + "=" + value + "; expires=" + date.toGMTString() + ";path=/";
document.cookie
} //获取某一cookie key=value
CookieHelper.prototype.getCookie = function (name) {
var strCookie = document.cookie;
var arrCookie = strCookie.split("; ");
for (var i = 0; i < arrCookie.length; i++) {
var arr = arrCookie[i].split("=");
if (arr[0] == name) {
return arr[1];
}
}
return "";
}
//获取cookie 类型 key=id=123&name=qwe&pwd=uio
CookieHelper.prototype.getCookies = function (name) {
var strCookie = document.cookie;
var arrCookie = strCookie.split("; ");
for (var i = 0; i < arrCookie.length; i++) {
var arr = arrCookie[i].split("=");
if (arr[0] == name) {
return arrCookie[i].substring(arrCookie[i].indexOf("=") + 1)
}
}
return "";
}
//获取所有cookie CookieHelper.prototype.getAllCookie = function (name) {
var strCookie = document.cookie;
var arrCookie = strCookie.split("; ");
if (arrcookie.length > 0) {
return arrCookie;
} else {
return "";
}
}

页面调用<script type="text/javascript" src="CookieHelper"></script>

在页面的js中调用上面封装的方法

//
function booking(id, num) {
if (parseInt(num == 0)) {
return false;
} else {
var startDate = document.getElementById("startDate").value;
var endDate = document.getElementById("endDate").value;
//声明封装方法存储Cookie
var cookiehelp = new CookieHelper();
var name="Bookingvalue";
var value = "UnitID=" + id + "&startDate=" + startDate + "&endDate=" + endDate;
//调用存储Cookie方法
cookiehelp.setCookie(name, value); var startDate = document.getElementById("startDate").value;
var endDate = document.getElementById("endDate").value;
window.location.href = "Booking.aspx?UnitID=" + id + "&startDate=" + startDate + "&endDate=" + endDate;
}
}

这样就完成了存储cookie。

这里存储的cookie是多参数的,调用时C#采用下图方式:

HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies["Bookingvalue"];
if (Cookie != null)
{
unitID = Convert.ToInt32(Cookie["UnitID"]);
}
if (unitID > )
{ if (!string.IsNullOrEmpty(HttpContext.Current.Request.Cookies["Bookingvalue"]["startDate"]))
{
if (Convert.ToDateTime(HttpContext.Current.Request.Cookies["Bookingvalue"]["startDate"]) >= DateTime.Now)
{
startDate = HttpContext.Current.Request.Cookies["Bookingvalue"]["startDate"];
}
else
{ }
}
if (!string.IsNullOrEmpty(HttpContext.Current.Request.Cookies["Bookingvalue"]["endDate"]))
{
if (Convert.ToDateTime(HttpContext.Current.Request.Cookies["Bookingvalue"]["startDate"]) >= Convert.ToDateTime(HttpContext.Current.Request.Cookies["Bookingvalue"]["endDate"]))
{
endDate = Convert.ToDateTime(startDate).AddDays().ToShortDateString();
}
else
{
endDate = HttpContext.Current.Request.Cookies["Bookingvalue"]["endDate"];
}
}
       }

很久很久之后,erp中在后台存储了个cookie,然后我激动的使用js去删除这个cookie,删除失败,我很困惑,究竟是方法不行还是人不行,最后检查时发现下图:

CookieHelper JS封装Cookie 存取方法

这个后台存的是session我居然天真的以为后台的方法名字叫什么就是什么功能,坑啊。

CookieHelper JS封装Cookie 存取方法

上一篇:将samba加入到windows域《转载》


下一篇:WCF初识