Reading cookie expiration date

前端 未结 7 1165
我在风中等你
我在风中等你 2020-11-28 11:50

Is it possible to read cookie expiration date using JavaScript?

If yes, how? If not, is there a source I can look at?

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 12:07

    There are some who will find this contentious - but one solution is to create another cookie at the same time and store the time/stamp in it parallel with whichever original cookie is created. Update them both at the same time and that way you can easily get the time from your new cookie (Alternatively append the time/stamp in your source cookie).

    The reason this would be contentious is that over the years the idea of storing cookies on a users PC isn't popular because you are taking up their space. However I really doubt a small timestamp cookie would be too horrific.

    Its worth remembering that if a time has passed then the browser will not report that cookie available. The browser may show the cookie present but when JS tries to access it - it won't be able too.

    Additionally I found that WebDeveloper toolbar in Firefox shows cookies that have passed but under Firefox > Privacy settings they are updated correctly.

提交回复
热议问题