6 month expiration date on a javascript cookie - javascript

I'm not great with javascript, but I have one that is working, but it seems like it's not following the 6 month expiration date that I set. Can someone help troubleshoot? This is what I have when setting the cookie's expiration date:
expDate = new Date;
// in the following line, 180 means 180 days.
expDate.setTime(expDate.getTime() + 180 * 24 * 60 * 60 * 1000);
expDate.toGMTString();
function setCookie(name, value, expires, path, domain, secure){
document.cookie= name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}
function getCookie(name){........
Any help troubleshooting would be perfect and very appreciated!
UPDATE - here is the script in its entirety. I'm calling a pop-up window only to show if it's been 3 page visits, and 20%, and 6 months expiration date. Just piecing it together so take it easy on me!!
expDate = new Date;
// in the following line, 180 means 180 days.
expDate.setTime(expDate.getTime() + 180 * 24 * 60 * 60 * 1000);
expDate.toGMTString();
function setCookie(name, value, expires, path, domain, secure){
document.cookie= name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}
function getCookie(name){
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1){
begin = dc.indexOf(prefix);
if (begin != 0) return null;}
else{begin += 2;}
var end = document.cookie.indexOf(";", begin);
if (end == -1){end = dc.length;}
return unescape(dc.substring(begin + prefix.length, end));
}
visits = getCookie('nVisits');
if (!visits) {
visits = 1
};
if (visits == 3)
if ((Math.random() * (100 - 1) + 1) < 20)
{
window.open("https://mypoup.html", "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");
}
if (visits < 3) {
++visits;
cookieData = visits;
setCookie('nVisits', cookieData, expDate)
}

Kaddath's comment solved the problem:
it seems that toGMTString() is obsolete and should not be used anymore, use toUTCString() instead.

Related

Expiration date on javascript cookie is not working -updated

UPDATE - made with suggestions from below:
I have the following script that pops up a window, after 3 visits, and to only 20% of visitors. The expiration date is set and shows in Developer tools for expiring in 180 days, but it's still popping up. What am I doing wrong? Any help is appreciated, I do not know javascript at all very well. This has to be done all in plain javascript - no jQuery
//setting the expiration date to 6 months from now//
expDate = new Date;
// in the following line, 180 means 180 days.//
expDate.setTime(expDate.getTime() + 180 * 24 * 60 * 60 * 1000);
expDate.toGMTString();
//making the cookie//
function setCookie(name, value, expires, path, domain, secure) {
document.cookie = name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=/wmb" + path : "") +
((domain) ? "; domain=www.example.com" + domain : "") +
((secure) ? "; secure" : "");
}
function getCookie(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
} else {
begin += 1;
}
var end = document.cookie.indexOf(";", begin);
if (end == -1) {
end = dc.length;
}
return unescape(dc.substring(begin + prefix.length, end));
}
//cookie name is called CustomSurvey//
visits = getCookie('CustomSurvey');
if (!visits) {
visits = 1
};
if (visits < 4)
//Math.random is set so that 20% of those visits actually get the popup//
if ((Math.random() * (100 - 1) + 1) < 20) {
//get location of where this popup came from; also contains variable from SM that will trickle down into reporting to tell where it came from//
var currentLocation = window.location;
window.open("https:mysurvey.com" + "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");
}
if (visits < 3) {
++visits;
cookieData = visits;
setCookie('CustomSurvey', cookieData, expDate)
}
I changed to what you suggested (except the url - that part is just made up (the real one pops up). I did everything suggested except when I changed the visits > 3 , now nothing pops up (after clearing cache and clicking about 50 times.
This is what I have now:
//setting the expiration date to 6 months from now//
expDate = new Date;
// in the following line, 180 means 180 days.//
expDate.setDate(expDate.getDate() + 180)
//making the cookie//
function setCookie(name, value, expires, path, domain, secure){
document.cookie= name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=/wmb" + path : "") +
((domain) ? "; domain=ptoweb.uspto.gov" + domain : "") +
((secure) ? "; secure" : "");
}
function getCookie(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
} else {
begin += 1;
}
var end = document.cookie.indexOf(";", begin);
if (end == -1) {
end = dc.length;
}
return unescape(dc.substring(begin + prefix.length, end));
}
//cookie name is called CustomSurvey//
visits = getCookie('CustomSurvey');
if (!visits) {
visits = 1
};
if (visits > 3)
//Math.random is set so that 20% of those visits actually get the popup//
if ((Math.random() * (100 - 1) + 1) < 20) {
//get location of where this popup came from; also contains variable from SM that will trickle down into reporting to tell where it came from//
var currentLocation = window.location;
window.open("https://www.surveymonkey" + "?" + currentLocation, "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");
}
if (visits < 3) {
++visits;
cookieData = visits;
setCookie('CustomSurvey', cookieData, expDate)
}

Cannot set cookie to expire on browser close

I'm trying to set a cookie using javascript so it expires when the browser is closed.
I have the following function to do that:
function createCookie(value,days) {
var name = "name";
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
expires = "; expires=" + date.toUTCString();
}
var cookie = name + "=" + value + expires + "; path=/";
document.cookie = cookie;
}
I tried many ways found here and there on the web like setting the date to "", setting it to yesterday (in that case the cookie is not even added) and omitting "expires" completly. I tried on Firefox and Chrome checking that every process was stopped before opening again, but the cookie is alway there.
What am I missing?
I am using this function for my self. It will work for you i gess :)
function createCookie(name, value, expiresInX_days, path) {
var a = new Date;
var expires = expiresInX_days || 1;
a.setTime(Date.now() + (1000 * 60 * 60 * 24 * expires));
var pt = path ? " ; path=" + path + ";" : ";";
return (document.cookie = name + "=" + value + ";" + "expires=" + a.toUTCString() + pt) ? !0 : !1;
}
If you want to delete your cookie, you can use this:
function rmCookie(cookieName){
var a = new Date;
a.setTime(0);
return (document.cookie = cookieName + "=;" + a.toUTCString()) ? !0 : !1;
}
If you want get your cookie clean,
function getMyFuckingCookie(cookieName){
var a = document.cookie.replace(/; /g, ";").split(";"),
b = a.length,
c = {},
nm = cookieName || !1;
while (b--) {
var d = a[b].split(/=(.+)/);
c[d[0]] = d[1];
}
return (nm) ? c[nm] : c;
}

pop-up using javascript with visits and sample rate not working

I am trying to make a pop-up that only shows up:
after the third visit, and only 20% of the time.
I have this right now, and I'm not really great at Javascript and need some help figuring out exactly what I'm doing wrong. Any help would be appreciated
visits = getCookie('nVisits');
if (!visits) {
visits = 1
}
if (visits == 3) {
deleteCookie('nVisits')
if (rand(0, 100) <= 20) {
window.open("http://mypopup.html", "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");
} else {
if (visits < 3) {
++visits;
cookieData = visits;
setCookie('nVisits', cookieData, expDate)
}
}
}
In the head of the page I set the cookie:
expDate = new Date;
// in the following line, 180 means 180 days.
expDate.setTime(expDate.getTime() + 180 * 24 * 60 * 60 * 1000); expDate.toGMTString();
function setCookie(name, value, expires, path, domain, secure){
document.cookie= name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}
function getCookie(name){
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1){
begin = dc.indexOf(prefix);
if (begin != 0) return null;}
else{begin += 2;}
var end = document.cookie.indexOf(";", begin);
if (end == -1){end = dc.length;}
return unescape(dc.substring(begin + prefix.length, end));
}
function deleteCookie(name, path, domain){
if (getCookie(name)){
document.cookie = name + "=" +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";}
}
----UPDATE---
I originally had this - which was working - it's just getting the random 20% to work...
<Script>
visits = getCookie('nVisits');
if (!visits){visits = 1};
if (visits == 3 ){deleteCookie('nVisits')
window.open("https://popup.html", "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");
}
if (visits < 3){++visits;
cookieData = visits;
setCookie('nVisits',cookieData,expDate)
}
</Script>
Because I couln't find the definition for your function rand, I completely rewrote the snipped.
const visitsCookieName = 'nVisits';
let visits = getCookie(visitsCookieName) || 0;
if (visits === 3) {
deleteCookie(visitsCookieName)
if (Math.floor(Math.random() * 5)) return;
window.open("http://mypopup.html", "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");
return;
}
setCookie(visitsCookieName, ++visits, expDate);
Explanation
The random logic I'm using here is really simple. First I multiply the random float to 5 which return a float number between 0 and ~4.99. After that we need the floored value of it. So it's between 0 and 4. Because 0 == false this hit only 20% of all tries.
Another problem in your snipped is on line 10 (if (visits < 3) {). This will never be true because in the upper statement you check if if (visits == 3) { (line 5) and because you didn't change the value of visits it can't be anything else but exactly 3.

Turn cookie expire in minutes/hours that expires in days

I have java script, i used it for create cookie and i want to exipre it in 5hours but minimum expire time is one day..Can any one help me to turn this code to expire cookies in minutes/hours? I tried but I failed.Help me..
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;
}
username='20';
setCookie("username",username,1);
1 is for one day,how to make it expiries in 5 hours?
I didn't try. I hope it will work.
exdays = 5 (hours) * 60 (minutes) * 60 (seconds);
exdate.setTime(exdate.getTime() + exdays);
The most direct conversion would be setting the hours with setHours isntead of setting the day with setDate
exdate.setHours(exdate.getHours() + time_in_hours )
You'll need a console open to view the two dates this outputs, but this would work (using .setHours()).
function setCookie(c_name, value, exhours)
{
var exdate = new Date();
var c_value = escape(value) + ((exhours == null) ? "" : "; expires=" + exdate.toUTCString());
console.log(c_value);
exdate.setHours(exdate.getHours() + exhours);
var c_value = escape(value) + ((exhours == null) ? "" : "; expires=" + exdate.toUTCString());
console.log(c_value);
document.cookie = c_name + "=" + c_value;
}
username = '20';
setCookie("username",username,5);
http://jsfiddle.net/MK5NH/
function setCookie(c_name,value,exTime)
{
var exdate=new Date();
exdate.setTime(exdate.getTime() + exTime);
var c_value=escape(value) + ((exTime==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
setCookie("username",username,3600000);
This will set your cookie for 1 hour.

javascript getting setting deleteing cookies

I am having trouble setting and deleting cookies in php Ive got no problem.. in javascript eh.. lots I know this question is everywhere but all the answers I come across seem to bring me the same fate so I have no idea whats going on.. this is what I have currently.
SetCookie("username",ztsUser,14,'/', '');
function SetCookie (name,value,expires,path,domain,secure) {
document.cookie = name + "=" + escape (value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}
the error:
expires.toGMTString is not a function
The whole lot of cookie functions I have.. is:
var today = new Date();
var expiry = new Date(today.getTime() + 365 * 24 * 60 * 60 * 1000);
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1) { endstr = document.cookie.length; }
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg) {
return getCookieVal (j);
}
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function DeleteCookie (name,path,domain) {
if (GetCookie(name)) {
document.cookie = name + "=" +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
}
function SetCookie (name,value,expires,path,domain,secure) {
document.cookie = name + "=" + escape (value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}
if anyone knows a better method Im all ears.. but this is what I got at the moment, I need to set, update, delete, get the value of.. cookies
toGMTString has been deprecated. Try toUTCString instead:
function SetCookie (name,value,expires,path,domain,secure) {
document.cookie = name + "=" + escape (value) +
((expires) ? "; expires=" + expires.toUTCString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}
You're also passing a number (14) when the function expects a Date (expires.toUTCString):
SetCookie("username", ztsUser, 14, '/', '');
What is 14 intended to be? 14 days? Assuming that, you can add this at the top of the function to support both numbers and dates:
if ('number' === typeof expires) {
expires = new Date(new Date().getTime() + expires * 86400000);
}
86400000 is the number of milliseconds in a day -- 24 * 60 * 60 * 1000.
There are already a number of great libraries for handling cookies in JS. Why not use one of them? Just go to your favorite search engine and enter "javascript cookie library" to find one.

Categories

Resources