I'm setting a cookie but the problem is that when I look at the expiration date in the Chrome inspector, it's only showing as a session cookie. This is my code:
var ExpirationDate = new Date();
ExpirationDate.setDate(ExpirationDate.getDate() + 400);
document.cookie = 'eucookie=2;' + ExpirationDate.toUTCString();
What do I need to change to make it expire in 13 months (400 days) instead of at the end of the session.
You need to tell it that it's the expires property you're setting:
document.cookie = 'eucookie=2;expires=' + ExpirationDate.toUTCString();
Related
I have a web-page that uses cookies. I've set the expiry on the cookies to 365 days and in the developer console I can see that the expiry is correct. But only on mobile the cookies expire within about a week, instead of a year. From what I've read this has to do with mobile browser sessions but I didn't find a viable solution using JQuery or vanilla JS.
If possible I would like to refrain from switching over to client side session storage.
This is the way I set my cookies:
var setCookie = function(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires +" ;"/*path=/"/;*/
}
Taken from here: https://www.w3schools.com/js/js_cookies.asp
I'm trying to set a cookie that will expire in a given amount of time. When the cookie is active (i.e. not expired) a div will be displayed. Once the time has expired the div will not be displayed and never again be displayed.
The cookie needs to be set on first page load and the expire date set.
I don't think the cookie needs to ever be deleted because that would then cause the cookie to be recreated.
So I guess I need to somehow check if the cookie has expired, not if it still exist. I can't find any information on how to check if a cookie has expired, only if it exist.
I'm new at cookies but here is my latest attempt:
if (document.cookie.indexOf("test=") >= 0) {
}
else {
// set a new cookie
expiry = new Date();
expiry.setTime(expiry.getTime()+(2*60*1000)); // Two minutes
// Date()'s toGMTSting() method will format the date correctly for a cookie
document.cookie = "test=" + expiry.toGMTString() +"; expires=" + expiry.toGMTString() +"; path=/";
if (document.cookie.indexOf("test=" + expiry.toGMTString() +"")){
//stuff here
}
}
I know it's probably way off. Also I have js.cookie.js installed just in case I needed it.
I want cookie to expire when session of user expires. It can be pure in javascript, XPages-SSJS.
On each page load set the cookie to expire on the current time + the length of the session.
For example, if your session is 1 hour long, add this on page load:
var time = new Date().getTime(); // get the current time
time += 3600 * 1000; // add 1 hour to the current time
document.cookie = 'cookiedata=' + cookiedata + '; expires=' + time.toGMTString() + ';';
In this example, cookiedata is whatever you are storing in the cookie.
The code on this XSnippet can be used behind a Logout button. This will clear the sessionScope variables for an 8.5.x server, but will not for a 9.0 server:
http://openntf.org/XSnippets.nsf/snippet.xsp?id=clear-session-whole-server
If you want to clear the sessionScope map on R9, you'll need to get a handle on the map, iterate the keys and clear them, as in this XSnippet:
http://openntf.org/XSnippets.nsf/snippet.xsp?id=clear-session-current-nsf
Here is an example how to modify the expiration date of the session cookie from server side:
<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
<xp:this.beforeRenderResponse>
<![CDATA[#{javascript:var sessionId = facesContext.getExternalContext().getRequest().getSession().getId();
var response = facesContext.getExternalContext().getResponse();
var maxAge = facesContext.getApplication().getApplicationProperty("xsp.session.timeout", "30");
var maxAge = maxAge * 60;
response.addHeader("Set-Cookie","SessionID=" + sessionId + "; path=/; max-age=" + maxAge)}]]>
</xp:this.beforeRenderResponse>
</xp:view>
But keep in mind that setting an expiration of a cookie will have some side-effects, f.e. if you are closing the browser the cookie will not be removed automatically. Because the cookie is is valid for the whole server (path=/), this can affect other applications running on the same path too.
onClick="javascript:document.cookie='n=1'"
Im new in javascript
I have a btn click will set cookie, how can I set expire time 1 hour on this cookie?
When you write the cookie to the browser, you need to specify an expiration date or a max age. However, note that max-age is ignored by Interent Explorer 8 and below. So if you're expecting to get usage from that browser, you can just rely on expires.
Example:
<script type="text/javascript">
function setMyCookie() {
var now = new Date();
var expires = new Date(now.setTime(now.getTime() + 60 * 60 * 1000)); //Expire in one hour
document.cookie = 'n=1;path=/;expires='+expires.toGMTString()+';';
}
</script>
And your button can call this function like so:
<input type="button" onclick="setMyCookie();">Set Cookie</input>
Note that I've also included the path to indicate that this cookie is site-wide.
You can read more about expiring cookies with the date or max-age here:
http://mrcoles.com/blog/cookies-max-age-vs-expires/
You can do:
onClick="setupCookie();"
function setupCookie() {
document.cookie = "n=1";
setTimeout(function() {
document.cookie = "n=0";
}, 3600000); // 1 hour
}
On click you can call some javascript function and while creating cookie itself you can set expire time please refer this
javascript set cookie with expire time
I have found a weird bug in my application and due to my small experience with Javascript I couldn't debug it;
I am trying to set a persistent cookie, which will die after one year from its set (max value in major browsers) but persists and won't be deleted after the browser gets closed. I've been using this code:
// Build the expiration date string:
var expiration_date = new Date();
expiration_date.setYear (expiration_date.getYear () + 1);
expiration_date = expiration_date.toGMTString();
// Build the set-cookie string:
var cookie_string = "test_cookies = true; path=/; expires=" + expiration_date;
// Create/update the cookie:
document.cookie = cookie_string;
I've noticed that the cookie has a session tag when I use cookie manager plugin, and only the ones with this tag get removed when the browser shuts down (others like Wordpress's and such scripts persist).
I changed your syntax over to my style of coding (variables at the top, minimal re-casting, etc.) and the example below works on my localhost quite well.
// Build the expiration date string:
var expiration_date = new Date();
var cookie_string = '';
expiration_date.setFullYear(expiration_date.getFullYear() + 1);
// Build the set-cookie string:
cookie_string = "test_cookies=true; path=/; expires=" + expiration_date.toUTCString();
// Create or update the cookie:
document.cookie = cookie_string;
If you are having problems on a production server, try setting the domain of the cookie as well (www.quirksmode.org/js/cookies.html#link5)
You can also use the max-age attribute.
cookie_string = "test_cookies=true; path=/; max-age=31536000";
One week: max-age=604800
One month: max-age=2628000
One year: max-age=31536000
have you tried using the getFullYear() and setFullYear() methods of the Date instance instead of getYear() and setYear() ? the latter are are deprecated, see here.
hope that helps! cheers.