I am using a function which sets a cookie. This function allows the cookie name, the cookie value and an additional expiry date of the cookie to be passed into it.
function setCookie(name, value, exdate) {
var c_value = escape(value) +
((exdate === null || exdate === undefined) ? "" : "; expires=" + exdate);
document.cookie = name + "=" + c_value;
};
Usage:
setCookie("my-cookie-name","my-value","Sun, 15 Jul 2012 00:00:01 GMT");
I have used the function with the date format above and believe it is cross browser compatible as I have tested if the cookie remains after closing various browsers and reopening them. I discovered that there were problems when using a format like "15 Jul 2012"
. This format worked for me during development in Firefox, but other browsers only seemed to set the cookie as a session cookie.
Should I stick to using just this format: "Sun, 15 Jul 2012 00:00:01 GMT"
or are there other formats I could use for the expiry date that will work across the major browsers (IE 7-9, Firefox, Chrome, Opera, Safari)?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…