admin管理员组文章数量:1134082
I am setting a cookie with JavaScript and it is working fine but it is not taking the expire time I am giving. It keeps on taking session value regardless of what I give, below is the code which I took from here
var now = new Date();
var time = now.getTime();
var expireTime = time + 1000*60;
now.setTime(expireTime);
var tempExp = 'Wed, 31 Oct 2012 08:50:17 GMT';
document.cookie = aaa+'='+sStr+';expires='+now.toGMTString()+';path=/';
I tried giving hard-coded value but still it is showing expire as session in chrome dev tool
var tempExp = 'Wed, 31 Oct 2012 08:50:17 GMT';
document.cookie = aaa+'='+sStr+';expires='+tempExp+';path=/';
Any idea what I am doing wrong?
I am setting a cookie with JavaScript and it is working fine but it is not taking the expire time I am giving. It keeps on taking session value regardless of what I give, below is the code which I took from here
var now = new Date();
var time = now.getTime();
var expireTime = time + 1000*60;
now.setTime(expireTime);
var tempExp = 'Wed, 31 Oct 2012 08:50:17 GMT';
document.cookie = aaa+'='+sStr+';expires='+now.toGMTString()+';path=/';
I tried giving hard-coded value but still it is showing expire as session in chrome dev tool
var tempExp = 'Wed, 31 Oct 2012 08:50:17 GMT';
document.cookie = aaa+'='+sStr+';expires='+tempExp+';path=/';
Any idea what I am doing wrong?
Share Improve this question edited Mar 15, 2022 at 13:05 isherwood 61k16 gold badges120 silver badges168 bronze badges asked Oct 31, 2012 at 9:14 antnewbeeantnewbee 1,9494 gold badges28 silver badges41 bronze badges 4- how are you checking the time of expiration of your cookie? – polin Commented Oct 31, 2012 at 9:52
- polin: in chrome press F12 (alternatively go to tools-> developer tools). It will open the frame below, there you can check. – antnewbee Commented Oct 31, 2012 at 10:02
- I know how it comes. My question is can you see the expiration time. I've tried console.log(document.cookie) but there you can see the cookie not the expiration time – polin Commented Oct 31, 2012 at 10:11
- Yes in 'Expires' column(after path column) it shows the date and time when the cookie will expire. But in my case it shows 'session' which means it will expire when I close the browser – antnewbee Commented Oct 31, 2012 at 11:02
10 Answers
Reset to default 96I've set the time to 1000*36000.
function display() {
var now = new Date();
var time = now.getTime();
var expireTime = time + 1000*36000;
now.setTime(expireTime);
document.cookie = 'cookie=ok;expires='+now.toUTCString()+';path=/';
//console.log(document.cookie); // 'Wed, 31 Oct 2012 08:50:17 UTC'
}
Below are code snippets to create and delete a cookie. The cookie is set for 1 day.
// 1 Day = 24 Hrs = 24*60*60 = 86400.
By using max-age:
- Creating the cookie:
document.cookie = "cookieName=cookieValue; max-age=86400; path=/;";
- Deleting the cookie:
document.cookie = "cookieName=; max-age=- (any digit); path=/;";
By using expires:
- Syntax for creating the cookie for one day:
var expires = (new Date(Date.now()+ 86400*1000)).toUTCString(); document.cookie = "cookieName=cookieValue; expires=" + expires + ";path=/;"
Here's a function I wrote another application. Feel free to reuse:
function writeCookie (key, value, days) {
var date = new Date();
// Default at 365 days.
days = days || 365;
// Get unix milliseconds at current time plus number of days
date.setTime(+ date + (days * 86400000)); //24 * 60 * 60 * 1000
window.document.cookie = key + "=" + value + "; expires=" + date.toGMTString() + "; path=/";
return value;
};
document.cookie = "cookie_name=cookie_value; max-age=31536000; path=/";
Will set the value for a year.
I use a function to store cookies with a custom expire time in days:
// use it like: writeCookie("mycookie", "1", 30)
// this will set a cookie for 30 days since now
function writeCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
I'd like to second Polin's answer and just add one thing in case you are still stuck. This code certainly does work to set a specific cookie expiration time. One issue you may be having is that if you are using Chrome and accessing your page via "http://localhost..." or "file://", Chrome will not store cookies. The easy fix for this is to use a simple http server (like node's http-server if you haven't already) and navigate to your page explicitly as "http://127.0.0.1" in which case Chrome WILL store cookies for local development. This had me hung up for a bit as, if you don't do this, your expires key will simply have the value of "session" when you investigate it in the console or in Dev Tools.
Use like this (source):
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+"; path=/";
}
Your browser may be configured to accept only session cookies; if this is your case, any expiry time is transformed into session by your browser, you can change this setting of your browser or try another browser without such a configuration
Remember that some browsers as Safari, will not allow you to set a cookie expiration bigger than 1 week. This is due to Safari's ITP: https://webkit.org/blog/9521/intelligent-tracking-prevention-2-3/
Basically, this will block the expiration time if you create/modify a cookie through document.cookie property. Only cookies created/modified on the server side will be able to have a bigger expiration time.
Other browsers could do the same thing in incognito mode, even with a shorter expiration time, only for the session.
i think the best way to expires the cookie is to just make the token null and set the expire time to now
code:
res.cookie("token", null, {
expires: new Date(Date.now() - 1000),
httpsOnly: true,
}
本文标签: javascriptHow can I set a cookie with expire timeStack Overflow
版权声明:本文标题:javascript - How can I set a cookie with expire time? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736799116a1953411.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论