這裏是一個簡單的庫我寫的高壓..你不需要臃腫庫..只是再壓縮這
var cookieManager = (function() {
return {
createCookie: function (name, value, days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
}
document.cookie = name + "=" + value + expires + "; path=/";
},
writeSessionCookie: function (cookieName, cookieValue) {
document.cookie = cookieName + "=" + cookieValue + "; path=/";
},
readCookie: function (name) {
var nameEq = name + "=";
var ca = document.cookie.split(';');
var i;
for (i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) === ' ') { c = c.substring(1, c.length); }
if (c.indexOf(nameEq) === 0) { return c.substring(nameEq.length, c.length); }
}
return null;
},
deleteCookie: function (name) {
this.createCookie(name, null, -1);
}
};
}());
用法:
cookieManager.writeCookie("foo","bar");
var fooValue=cookieManager.readCookie("foo");
這看起來相當不錯,雖然我不能似乎在Firefox中看到任何會話數據。 – user896428
會話數據是什麼意思? – ShankarSangoli
我的意思是cookie數據。使用Firefox開發者工具。該cookie似乎沒有被設置。 – user896428