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/
Related
I'm trying to set cookies that are permanent in Microsoft edge. They last only until I close the browser. Yes I have cookies enabled. Here is some code I found from a while ago:
var cookieHandler = {
setCookie : function(name, value, days, time)
{
var expires = "";
if(days)
{
var date = new Date();
date.setTime(date.getTime() + (time || (days * 24 * 60 * 60 * 1000)));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
},
getCookie : function(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;
},
eraseCookie : function(name)
{
document.cookie = name + '=; Max-Age=-99999999999;';
}
};
Yeah when I reopen the browser my cookies are gone. Does anyone have working code for cookies in Microsoft edge?
Edit:
I'm using the file:/// origin.
I created javascript cookie. But i can't read that cookie in wordpress. setcookie() function also not working. Only $_COOKIE is working .But it expiring in one minute.
You can use following javascript function to create, read and delete cookies
CREATE COOKIE
`
function createCookie(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 = encodeURIComponent(name) + "=" + encodeURIComponent(value) + expires + "; path=/";
}
`
READ COOKIE
`
function readCookie(name) {
var nameEQ = encodeURIComponent(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 decodeURIComponent(c.substring(nameEQ.length, c.length));
}
return null;
}
`
DELETE COOKIE
`
function eraseCookie(name) {
createCookie(name, "", -1);
}
`
Now, call javascript functions as:
createCookie('test','hello','5');
readCookie('test');
eraseCookie('test');
I have a timed cookie that I need to change to a session cookie, I'm not quite sure how to modify it, any help would be appreciated.
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=/";
}
function readCookie(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 have difficulties setting the cookie time to reset after 1 week. Does anyone know how to achieve this? I am using the following code in my Shopify theme. I can now only select to show it once or show it always in my theme settings.
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;
}
function getCookie(cname) {
var name = cname + "=";
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);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
Looking forward to your replies. Cheers!
Try something like this? Takes the minutes in a week. multiplies it by 60 to turn it into seconds.
var d = new Date();
var minutes = 10080;
d.setTime(d.getTime() + (minutes * 60));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
I'm creating an add [ITEM] feature to my site that will include cookies.
Now the Add [ITEM] part works but I need an Remove [ITEM].
This is my code:
$(window).load(function(){
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,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;
}
$(document).ready(function(){
var cookieyess=getCookie("save");
if(cookieyess!==undefined&&cookieyess!==null&&cookieyess!==""){
$('#save1').show();
}
else{
$('#save1').hide();
}
});
$('#save_1').click(function(){
var cookieyes=getCookie("save");
if(cookieyes!==undefined&&cookieyes!==null&&cookieyes!==""){
$('#save1').show();
}
else{
$('#save1').show();
setCookie("save","yes",365);
}
});
});
Now I need to make a function so when you click Removes it deletes that Cookie.
To delete mycookie, this should work by setting an expiration date in the past:
setCookie("mycookie", "", -1)
Here is a small library that I wrote:
var myCookieHandler = (function () {
return {
createCookie: function (name, value, days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
}
document.cookie = name + "=" + value + expires + "; path=/";
},
readCookie: function (name) {
var nameEq = name + "=";
var ca = document.cookie.split(';');
var i;
for (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;
},
deleteCookie: function (name) {
this.createCookie(name, null, -1);
}
};
}());
usage:
myCookieHandler .writeCookie("Login","true",2);
var cookieValue=myCookieHandler.readCookie("Login");
myCookieHandler.deleteCookie("Login");
Hope this helps..