Cookies disappear after setting them in Microsoft edge - javascript

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.

Related

Website not getting cookie info correctly and spamming reload

I'm trying to define a version for my website so it can reload the resources if the versions don't match. The problem is that when I fetch the cookie, the cookie value is not returned. The result of this is that the website is in a constant state of reloading.
I made sure that the cookie was there and I tried using my functions in the console and setting the cookie manually.
Webpage:
var version = '1.0.0';
var cVersion = version == getCookie("version");
if(!cVersion) {
setCookie("version", version, 1000);
window.location.reload(true);
}
Cookies 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=/";
}
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.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 "";
}
The website should reload and update the version number if they are different and do nothing if they are the same.

Cookies cannot read in wordpress

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

Modifying timed cookie to session

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

How to conver these exact create/read cookie functions to localstorage?

I want to replace my current functions of creating and reading cookie, and save it into the local storage instead.
How can I replicate this for local storage instead?
function createCookie(name, value, days) {
var c_date,
c_name = name + "=" + value + ";",
c_expi = "",
c_domain = "domain=.domain.com;",
c_path = "path=/";
if (days > 0) {
c_date = new Date();
c_date.setTime(c_date.getTime() + (days * 24 * 60 * 60 * 1000));
c_expi = "expires=" + c_date.toGMTString() + ";";
}
// create the cookie
document.cookie = c_name + c_expi + c_domain + c_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;
}
Using a LocalStorage is to easy then store a cookie value .. you need to just write
// Store
localStorage.setItem("lastname", "Smith"); // here is lastname is your key
// Retrieve
document.getElementById("result").innerHTML = localStorage.getItem("lastname");
If you want to check that Browser support local
Storage then .
if(typeof(Storage) !== "undefined") {
// Code for localStorage/sessionStorage.
} else {
// Sorry! No Web Storage support..
}
but before use localStoreage you must read
HTML 5 Storage's same origin policy
you can also store javascript object as JSON
function createCookie(name, value, days) {
var c = {
domain = ".domain.com",
path = "path=/"
}
c[name] = value;
if (days > 0) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
c.expi = date.toGMTString();
}
//local storaage
localStorage.setItem('somename', JSON.stringify(c));
}
function readCookie(name) {
var c = JSON.parse(localStorage.getItem('somename') || '{}');
return c[name];
}

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/

Categories

Resources