How Can I Set The Cookie Expire Every 30 Seconds [duplicate] - javascript

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

Related

document.cookie sets the wrong path [duplicate]

I am saving some cookie values on an ASP page. I want to set the root path for cookie so that the cookie will be available on all pages.
Currently the cookie path is /v/abcfile/frontend/
Please help me.
simply: document.cookie="name=value;path=/";
There is a negative point to it
Now, the cookie will be available to all directories on the domain it
is set from. If the website is just one of many at that domain, it’s
best not to do this because everyone else will also have access to
your cookie information.
For access cookie in whole app (use path=/):
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
Note:
If you set path=/,
Now the cookie is available for whole application/domain.
If you not specify the path then current cookie is save just for the current page you can't access it on another page(s).
For more info read- http://www.quirksmode.org/js/cookies.html (Domain and path part)
If you use cookies in jquery by plugin jquery-cookie:
$.cookie('name', 'value', { expires: 7, path: '/' });
//or
$.cookie('name', 'value', { path: '/' });
document.cookie = "cookiename=Some Name; path=/";
This will do
See https://developer.mozilla.org/en/DOM/document.cookie for more documentation:
setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/.test(sKey)) { return; }
var sExpires = "";
if (vEnd) {
switch (typeof vEnd) {
case "number": sExpires = "; max-age=" + vEnd; break;
case "string": sExpires = "; expires=" + vEnd; break;
case "object": if (vEnd.hasOwnProperty("toGMTString")) { sExpires = "; expires=" + vEnd.toGMTString(); } break;
}
}
document.cookie = escape(sKey) + "=" + escape(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
}
This will help....
function setCookie(name,value,days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return
c.substring(nameEQ.length,c.length);
}
return null;
}

How to Delete My JavaScript Cookies?

I have created javascript functions to set and delete cookies.
setCookie is working perfectly but eraseCookie is not working.
HTML
<a onclick="setCookie('analystics');">Allow</a>
<a onclick="eraseCookie('analystics')">Deny</a>
<p>Click on this paragraph.</p>
JavaScript
function setCookie(cname,cvalue,exdays) {
var d = new Date();
d.setTime(d.getTime() + (30 * 1000));
var expires = "expires=" + d.toGMTString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
var values = document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
doSomething(cname);
}
function doSomething(cname) {
console.log(cname);
var myCookie = cname;
if (myCookie == null) {
alert('null');
} else {
alert('defined');
$("p").click(function() {
alert("The paragraph was clicked.");
});
}
}
Cookie Delete Function:
function eraseCookie(cname) {
setCookie(cname, '', -1);
alert('The cookie has been successfully erased.');
}

Set Expires Time in Cookie Javascript

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=/';
}

SetCookie and expire time in one minute

I want to set input value in cookies and want alert in click function. Also I want to expiration date to cookies. I worked on cookies function but it is giving blank value. fiddle
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;
}
Try this instead:
Set method:
function setCookie(name, value, days)
{
var expires;
if (days)
{
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
}
else
{
expires = "";
}
document.cookie = name + "=" + value + expires + "; path=/";
};
Get method:
function getCookie(name)
{
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++)
{
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
};
I've changed the jsfiddle respectively: http://jsfiddle.net/DVeVm/

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.

Categories

Resources