JS Client Side Cookies Prematurely Expire on Mobile Browsers - javascript

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

Related

Cookie expiration only working for session cookie

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();

Javascript setting cookies with path=/

I'm creating cookies which are intended to be shared all across mysite.
This is the code that creates such 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();
var path ="path=/;";
document.cookie = cname + "=" + cvalue + ";" + expires + ";" + path;
};
It looks pretty straight forward, and I'm using path=/ to indicate that I want to create or modify always the same cookie all along my site.
The problem is that it is creating one cookie for each URL. With a Mozilla plugin I can see the following:
Cookie Name Value Path
timer_is_enabled true /
timer_is_enabled false /foo
timer_is_enabled true /foo/bar
Which is causing my many bugs because the variables which are being accessed are not one and only, but many independent ones.
Any idea why I'm getting this behavior?
Your code should work as expected, at least regarding the path attribute. Those other cookies may be remnants from earlier tests (sadly, there's normally no way to track the creation date of a given cookie since browsers don't normally keep such information).
I suggest you remove all current cookies from the browser and try again.
That function works ok for me. Ran the following:
setCookie('myCookieKey', 'myCookieValue', 10);
And I got the following:

How to edit existing cookies in subdomains?

If a subdomain of lol.com has a cookie named "a" with value "a" and that has a domain of .lol.com how would you edit that cookie from lol.com if you have a console open in it (using JS console from inspect element in chrome).
Also would it be possible to edit it too if the cookie's domain was a.lol.com? They are both https://
Thank you.
Before adding a new cookie you must assign it to specific domain.
Example:
This function helps you to easily add cookies:
function setCookie(cName, cValue, cExpireInDays, cDomain){
var d = new Date();
d.setTime(d.getTime() + (cExpireInDays*24*60*60*1000));
var expires = "expires="+ d.toUTCString();
document.cookie = cName + "=" + cValue + "; " + expires + ";domain="+cDomain+";";
}
From sub.lol.com open console and add new cookie by calling the setCookie function
setCookie("Test", "TestValue", 4, "lol.com");
Go to lol.com open another time the console, and write document.cookie you will get the following data:
"Test=TestValue; OTHER_COOKIES..."
If you want edit it you must add a new cookie with the same domain as we did before or just call the setCookie function.

Javascript Cookies - adding multiple cookies instead of updating one

I am trying to add a popup pause in one day using JS cookies. Here is teh code:
if (document.cookie.indexOf("expirator") >= 0) {
// do not show popup
}
else{
var d = new Date();
var start_date = d.toUTCString();
d.setTime(d.getTime() + (1*24*60*60*1000));
var expireDate = "expires="+d.toUTCString();
document.cookie = "expirator=1;" + expireDate + "; domain=a5w.org; path=/";
// show popup
}
for a strange reason this scipt adds multiple new cookies with the same name ("expirator") instead of updating one. why is that so?
screenshot from FF cookies:
http://a5w.org/up/uploads/mike/2015-10-10/1444519970_cookies.png
You don't update cookies; you need to overwrite them.
Two cookies may have the same name if they were set for different domains or paths.
https://stackoverflow.com/a/1242446/280842
EDIT
There seems to be a bug in Firefox that displays multiple cookies in Firefox cookie manager. See https://bugzilla.mozilla.org/show_bug.cgi?id=504086

How do i set my sessionid in my cookie to expire at the end of session?

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.

Categories

Resources