Javascript Cookie with no expiration date

前端 未结 9 883

I would like to set up a cookie that never expires. Would that even be possible?

 document.cookie = \"name=value; expires=date; path=path;domain=domain; secu         


        
相关标签:
9条回答
  • 2020-11-27 14:10

    You could possibly set a cookie at an expiration date of a month or something and then reassign the cookie every time the user visits the website again

    0 讨论(0)
  • 2020-11-27 14:12

    YOU JUST CAN'T. There's no exact code to use for setting a forever cookie but an old trick will do, like current time + 10 years.

    Just a note that any dates beyond January 2038 will doomed you for the cookies (32-bit int) will be deleted instantly. Wish for a miracle that that will be fixed in the near future. For 64-bit int, years around 2110 will be safe. As time goes by, software and hardware will change and may never adapt to older ones (the things we have now) so prepare the now for the future.

    See Year 2038 problem

    0 讨论(0)
  • 2020-11-27 14:17

    There is no syntax for what you want. Not setting expires causes the cookie to expire at the end of the session. The only option is to pick some arbitrarily large value. Be aware that some browsers have problems with dates past 2038 (when unix epoch time exceeds a 32-bit int).

    0 讨论(0)
提交回复
热议问题