开发者

Javascript delete cookie next day

开发者 https://www.devze.com 2022-12-30 11:56 出处:网络
I was wonder if you can delete a cookie at the beginning of the next day. Lets say I log in to a website at 开发者_JAVA技巧1:30pm and use it again throughout the day but at midnight or 1 minute past

I was wonder if you can delete a cookie at the beginning of the next day.

Lets say I log in to a website at 开发者_JAVA技巧1:30pm and use it again throughout the day but at midnight or 1 minute past, in the new day, the cookie is removed.

Many thanks for any help, C


Yes - set an expiry time of midnight:

function midnight_cookie(name, value, path)
{
  var now = new Date();
  var expire = new Date();
  expire.setFullYear(now.getFullYear());
  expire.setMonth(now.getMonth());
  expire.setDate(now.getDate()+1);
  expire.setHours(0);
  expire.setMinutes(0);
  document.cookie = name+"="+value+"; expires=" + expire.toString() +"; path=" + path;
}


Give the cookie an appropriate expiry datetime.


You can let cookies expire on a given date / time. Have a look at the setCookie function here:

http://techpatterns.com/downloads/javascript_cookies.php

0

精彩评论

暂无评论...
验证码 换一张
取 消