I have a string that I use to set expires date in cookie. But I always fail to set it. Here is my Code:
var expTime = '2016-06-09T03:06:53Z';
var valueCookie = 'test';
SetCookie('myCookie', valueCookie, expTime);
function SetCookie(name, value, expTime) {
document.cookie = name + '=' + value + '; ' 'expires=' + expTime+ '; path=/';
};
Why I fail to set Expire Date in Cookie?
Thanks
try this
function setCookie(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;
}
The parameters of the function above are the name of the cookie (cname), the value of the cookie (cvalue), and the number of days until the cookie should expire (exdays).
The function sets a cookie by adding together the cookiename, the cookie value, and the expires string.
here is my code
function setCookie(name,value,data){
var oDate =new Date();
oDate.setDate(oDate.getDate()+data);
document.cookie=name+'='+value+';expires='+oDate;
}
I think there is a syntax error.Try this
var expTime = '2016-06-09T03:06:53Z';
var valueCookie = 'test';
SetCookie('myCookie', valueCookie, expTime);
function SetCookie(name, value, expTime) {
document.cookie = name + '=' + value + '; expires=' + expTime+ '; path=/';
}
Related
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;
}
I want to apply css through javascript if cookie xxxexist - code:
<script>
function setCookie(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=/";
}
var kuki = document.cookie.indexOf('cookie_name=xxx');
alert(kuki);
if (kuki != -1)
{
document.getElementById("ads-back").style.display = "none";
document.getElementById("ffield").style.display = "none";
document.getElementById("bcd").style.display = "none";
}
else { setCookie(xxx, 1, 1) }
</script>
The problem is that I always receiving -1 it should after seting cookie setCookie(xxx, 1, 1) get different value?
setCookie(xxx, 1, 1)
As you are setting cookie name with xxx variable and finding it with cookie_name as below that is the issue..try to find the index of cookie actual name set then it will work.
var kuki = document.cookie.indexOf('cookie_name=xxx');
To save cookie you have to use following format
document.cookie = cname + "=" + cvalue + ";expires=" + expires + ";";
To get saved cookies you have to use following both ways because if you have saved more than one cookie your cookie may not be the first element of the cookie array so you have to check both 'cookie_name=xxx' and
' cookie_name=xxx'
var kuki1 = document.cookie.indexOf('cookie_name=xxx');
var kuki2 = document.cookie.indexOf(' cookie_name=xxx');
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);
This is my code:
jQuery(function ($){
var player = $('#player');
var time = 0;
function setCookie(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=/";
}
player.bind('pause', function(){
console.log('player was paused');
console.log(player.currentTime);
setCookie("time", player.currentTime, 2)
});
});
I declared two variables (an object and a number). The object player gets defined correctly, but when I try to call time the console returns undefined.
You have not declared global variables. Both the values will be undefined in global scope. To have a global scope you may define like this.
var player = $('#player');
var time = 0;
jQuery(function ($){
function setCookie(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=/";
}
player.bind('pause', function(){
console.log('player was paused');
console.log(player.currentTime);
setCookie("time", player.currentTime, 2)
});
});
You may really define it global?
jQuery(function ($){
var player = $('#player');
window.time = 0;
function setCookie(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=/";
}
player.bind('pause', function(){
console.log('player was paused');
console.log(player.currentTime);//what should curentTime be?
setCookie("time", player.currentTime, 2);
window.time=player.currentTime;//an example
});
});
Now it is really global...
//in the console
console.log(time);
I've been trying to add a variable when setting a cookie but no luck. The cookie is created but the value is not passed and it appears as +content+
function createCookie(name, value, expires, path, domain) {
var cookie = name + "=" + value + ";";
if (expires) {
// If it's a date
if(expires instanceof Date) {
// If it isn't a valid date
if (isNaN(expires.getTime()))
expires = new Date();
}
else
expires = new Date(new Date().getTime() + parseInt(expires) * 1000 * 60 * 60 * 24);
cookie += "expires=" + expires.toGMTString() + ";";
}
if (path)
cookie += "path=" + path + ";";
if (domain)
cookie += "domain=" + domain + ";";
document.cookie = cookie;
}
function updateCookie(){
var content = "value of cookie";
createCookie("newcookie", "+content+", new Date(new Date().getTime() + 10000));
}
I think you want
var content = "value of cookie";
createCookie("newcookie", content , new Date(new Date().getTime() + 10000));