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.
Related
Could someone update the following code to make the cookie expire in 30 seconds.
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;
}
function createCookie(name, value) {
var date = new Date();
date.setTime(date.getTime()+(30*1000));
var expires = "; expires="+date.toGMTString();
document.cookie = name+"="+value+expires+"; path=/";
}
You can specify the maximum age in seconds when setting a cookie:
function setCookie(name, value, maxAgeSeconds) {
var maxAgeSegment = "; max-age=" + maxAgeSeconds;
document.cookie = encodeURI(name) + "=" + encodeURI(value) + maxAgeSegment;
}
Usage:
setCookie("username", "blaise", 30);
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.
I'm trying to set a cookie with javascript which is valid for a whole domain. This works everywhere except on iOS where the cookie becomes valid only for the page it was set on. For example cookie set via mydomain.com/cat/123 is not found when trying to fetch it on mydomain.com/cat/338
var d = new Date();
d.setTime(d.getTime() + (7*24*60*60*1000));
expires = d.toUTCString();
document.cookie = 'abc_rem=true;' + ' expires=' + expires +'; domain=mydomain.se; path=/';
The problem appears in both Safari and Chrome on iOS. I have tried searching for answers to no avail. What am I doing wrong?
Thanks in advance!
The following works for me:
function setCookie() {
var c_name = "abc_rem";
var value = "true", exdays = 365;
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
var c=c_name + "=" + c_value + ";path=/";
document.cookie=c;
}
And this is how I get the cookie afterwards
function getCookie(c_name) {
var i,x,y,ARRcookies=document.cookie.split(";");
var found = false;
for (i=0;i<ARRcookies.length;i++) {
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name) {
found = true;
}
}
if (found) {
// do whatever you need to do here
}
}
I have a simple popup code , it opens two sites with one click.
Now the problem is that it doesn't work for every 24 Hours. It works many many times a day.
Where is wrong in my own code? How can i solve this ?
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name)
{
return unescape(y);
}
}
}
function setCookie(c_name,value){
var exdays= 1;
var exdate=new Date();
exdate.setHours(exdate.getHours() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
function checkCookie(){
var username1=getCookie("tabligh1");
var usernam = "sendshod";
if(username1==null){
window.open('#','_parent','toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1');
window.focus();
}
if(username1=="" | username1==null){
if(window.open('site adress1','_blank','toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1')){
window.focus();
setCookie("tabligh1",usernam);
}
}
if(username1=="" | username1==null){
if(window.open('site adress2','_blank','toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1')){
window.focus();
setCookie("tabligh1",usernam);
}
}
}
document.onclick = checkCookie;
if ((window.XMLHttpRequest == undefined) && (ActiveXObject != undefined)) window.onload = checkCookie;
Your setCookie() function is setting the cookie to expire 1 hour from now, not 24 hours from now.
As you can see here in your code:
function setCookie(c_name, value) {
var exdays = 1;
var exdate = new Date();
exdate.setHours(exdate.getHours() + exdays);
var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
document.cookie = c_name + "=" + c_value;
}
You are setting the expiration time to one hour from the current time. Perhaps you meant to increment the time by one day, not one hour?
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.