|
/* |
|
cookieUtil.js |
|
Manipulate cookies in JavaScript |
|
*/ |
|
var cookieUtil = { |
|
init: function () { |
|
this.enabled = this.cookiesEnabled() ? "Enabled" : "Disabled"; |
|
}, |
|
createCookie: function (name, value, days) { |
|
var date, expires; |
|
if (days) { |
|
date = new Date(); |
|
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); |
|
expires = "; expires=" + date.toGMTString(); |
|
} |
|
else { |
|
expires = ""; |
|
} |
|
document.cookie = name + "=" + value + expires + "; path=/"; |
|
}, |
|
readCookie: function (name) { |
|
var search = name + "=", ca = document.cookie.split(";"), i, c; |
|
for (i = 0;i < ca.length; i = i + 1) { |
|
c = ca[i]; |
|
while (c.charAt(0) === " ") { |
|
c = c.substring(1, c.length); |
|
} |
|
if (c.indexOf(search) === 0) { |
|
return c.substring(search.length, c.length); |
|
} |
|
} |
|
return null; |
|
}, |
|
deleteCookie: function (name) { |
|
this.createCookie(name, "", -1); |
|
}, |
|
deleteAllCookies: function () { |
|
if (document.cookie.length > 0) { |
|
var cs = document.cookie.split(";"), i, c, cname; |
|
for (i = 0; i < cs.length; i = i + 1) { |
|
c = cs[i].split("="); |
|
cname = c[0]; |
|
this.deleteCookie(cname); |
|
} |
|
} else { |
|
return false; |
|
} |
|
}, |
|
cookiesEnabled: function () { |
|
this.createCookie("test", "test"); |
|
var test = this.readCookie("test"); |
|
if (test === "test") { |
|
this.deleteCookie("test"); |
|
return true; |
|
} else { |
|
return false; |
|
} |
|
} |
|
}; |