admin管理员组文章数量:1327665
A form has the following code to prevent multiple form submissions:
function AllowNoDups() { var cookie_ls = document.cookie; if (cookie_ls.indexOf(document.location) > -1) {
alert("You've already submitted a free meter request. Thank you for your interest! ");
return false; } else { document.cookie = 'username=' + value; + 'expires=' + var now = new Date(); var time = now.getTime(); time += 3600 * 1000; now.setTime(time); document.cookie = 'username=' + value + '; expires=' + now.toGMTString() + '; path=/';; + 'path = /' document.cookie = "; path=/newform.php; expires=Sun, 1-Jan-2040 00:00:01 GMT;"; return true;};};
While the code works, I would rather set the cookie to expire after 24 hours have elapsed, as opposed to specifying a date at which the cookie would expire. How would I go about acplishing this? Thank you!
A form has the following code to prevent multiple form submissions:
function AllowNoDups() { var cookie_ls = document.cookie; if (cookie_ls.indexOf(document.location) > -1) {
alert("You've already submitted a free meter request. Thank you for your interest! ");
return false; } else { document.cookie = 'username=' + value; + 'expires=' + var now = new Date(); var time = now.getTime(); time += 3600 * 1000; now.setTime(time); document.cookie = 'username=' + value + '; expires=' + now.toGMTString() + '; path=/';; + 'path = /' document.cookie = "; path=/newform.php; expires=Sun, 1-Jan-2040 00:00:01 GMT;"; return true;};};
While the code works, I would rather set the cookie to expire after 24 hours have elapsed, as opposed to specifying a date at which the cookie would expire. How would I go about acplishing this? Thank you!
Share Improve this question asked Oct 18, 2012 at 19:05 user1736377user1736377 311 gold badge1 silver badge2 bronze badges 2- 1 What's to stop me from clearing my cookies and submitting again? My point is, make sure you do this check server-side as well if you aren't already. – TheZ Commented Oct 18, 2012 at 19:06
- Change time += 3600 * 1000; to time += (24*60*60*1000) – mplungjan Commented Oct 18, 2012 at 19:11
2 Answers
Reset to default 2You can use the max-age
attribute. See this wikipedia article.
As an alternative to setting cookie expiration as an absolute date/time, RFC 6265 allows the use of the Max-Age attribute to set the cookie’s expiration as an interval of seconds in the future, relative to the time the browser received the cookie.
Use this function and pass the parameter as per your requirement..
function callcookie(name,value,days){
if(days){
var d=new Date();
d.setTime(d.getTime()+(days*24*60*60*1000));
var expires = "; expires="+d.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
本文标签: javascriptSet cookie to expire in 24 hoursStack Overflow
版权声明:本文标题:javascript - Set cookie to expire in 24 hours - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742228658a2436805.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论