使Cookie在30秒后过期

时间:2011-10-24 18:04:44

标签: javascript cookies

有人可以更新以下代码,使Cookie在30秒后过期。

function setCookie(c_name, value, exdays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
    document.cookie = c_name + "=" + c_value;
}

2 个答案:

答案 0 :(得分:30)

function createCookie(name, value) {
   var date = new Date();
   date.setTime(date.getTime()+(30*1000));
   var expires = "; expires="+date.toGMTString();

   document.cookie = name+"="+value+expires+"; path=/";
}

答案 1 :(得分:2)

您可以指定设置Cookie时的最大年龄(以秒为单位):

function setCookie(name, value, maxAgeSeconds) {
    var maxAgeSegment = "; max-age=" + maxAgeSeconds;
    document.cookie = encodeURI(name) + "=" + encodeURI(value) + maxAgeSegment;
}

用法:

setCookie("username", "blaise", 30);