这样的cookie用JS怎样读取
C# code
HttpCookie cookie = Cookie.Get("user");
cookie = Cookie.Set("user");
cookie.Values["name"] = HttpContext.Current.Server.UrlEncode(name.Trim());
cookie.Values["petName"] = HttpContext.Current.Server.UrlEncode(petName.Trim());
cookie.Expires = DateTime.Today.AddDays((double)saveDays);
我在后台是这样存了cookie
前台我有一个地方要用JS来取它,请问要怎么取name和petName?
------解决方案--------------------
function getCookie() {
if (arguments.length==2){
var CookieFound = false;
var CookieString = getCookie(arguments[0]);
var aCookieString=CookieString.split('&');
for (var i=0;i<aCookieString.length;i++){
j=aCookieString[i].indexOf('=');
if (arguments[1]==aCookieString[i].substring(0,j))
return unescape(aCookieString[i].substr(j+1));
}
} else {
var CookieFound = false;
var CookieString = document.cookie;
var aCookieString=CookieString.split('; ');
for (var i=0;i<aCookieString.length;i++){
j=aCookieString[i].indexOf('=');
if (arguments[0]==aCookieString[i].substring(0,j))
return unescape(aCookieString[i].substr(j+1));
}
}
return '';
}