Below are the two functions I currently use inorder to read and create my cookie (cookie should only be valid for 1 day)
function set1DayValidationCookie(){
var d = new Date();
var today = d.getMonth() + '' + d.getDate();
if (readCookie('onedaycookie') != today) {
document.cookie = 'onedaycookie='+today;
console.log('cookie has been created');
} else {
alert('cookie already exist!');
}
}
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;
}
On refresh of the page, cookie is recognized however
for example, I change my url from www.mywebsite.com/en/ to www.mywebsite.com/ru/
(for language purposes) cookie has been created again.
I would like to ask what I am missing on this part?
You need to set the 'path' part of the cookie to '/'. Then it will Work for all paths.
document.cookie = "username=John Smith; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";
Witout the path, the cookie will be set for the current path only.
I would suggest this code
window.cookie = {
set: function(c_name, value, exdays, path = '/') {
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value = escape(value) + ((exdays == null) ? "" : ("; expires=" + exdate.toUTCString())) + "; path=" + path;
document.cookie = c_name + "=" + c_value;
},
get: function(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);
}
}
}
};
And you can check like this:
if("undefined" !== typeof cookie.get('lang')){
//cookie is not set
cookie.set('lang', 'en', 1);
}else{
//cookie is set
}
Related
So what im tryin to do is to check for a cookie existance (for example accepted=yes) If it is not set, it will return nothing and if not, it will execute some script and set the accepted=yes cookie. So that on the next visit the visitor wont see the popup.
var cookie = document.cookie;
if (cookie = accepted=yes) {
} else {
document.cookie = "accepted=yes";
}
That is the code i have discovered.
You can use getCookie and setCookie Functions
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 "";
}
Example
var user = getCookie("username");
if (user != "") {
alert("Welcome again " + user);
} else {
user = prompt("Please enter your name:", "");
if (user != "" && user != null) {
setCookie("username", user, 365);
}
}
Reference: W3Schools https://www.w3schools.com/js/tryit.asp?filename=tryjs_cookie_username
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 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;
}
So what I'm saying is that can you create a cookie without using JQuery and only JavaScript.
Do you have an answer?
I have these simple function to do that
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;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
Now, calling functions
createCookie('ppkcookie','testcookie',7);
var x = readCookie('ppkcookie')
Create cookies with javascript
JavaScript can create, read, and delete cookies with the document.cookie property.
With JavaScript, a cookie can be created like this:
document.cookie = "username=John Doe";
You can also add an expiry date (in UTC time). By default, the cookie is deleted when the browser is closed:
document.cookie="username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC";
With a path parameter, you can tell the browser what path the cookie belongs to. By default, the cookie belongs to the current page.
document.cookie="username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";
All together code:
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 "";
}
function checkCookie() {
var user = getCookie("username");
if (user != "") {
alert("Welcome again " + user);
} else {
user = prompt("Please enter your name:", "");
if (user != "" && user != null) {
setCookie("username", user, 365);
}
}
}
I tried to save a number in javascript in document.cookie.
But when i try to get a cookie with a number i'm always getting NaN when i try to display it
var playerCoffeeCount = 0;
function setCookie(c_name, value, exdays) { //Setting a cookie (just a shortcut for making cookies)
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 getCookie(c_name) { //Getting a cookie (another shortcut)
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 saveGame(){
setCookie("playerCoffeeCookie", playerCoffeeCount, 999);
}
function loadGame(){
playerCoffeeCount = getCookie("playerCoffeeCookie");
}
function displayCookie(){
document.getElementById("coffeecount").innerHTML= "Coffee: " + Math.floor(playerCoffeeCount);
}
This cookie system in JavaScript saves the value, but it displays as a NaN... Please help :(
try this, working good.
function createCookie(name,days,value) {
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) { //cookie reader function
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;
}
function eraseCookie(name) { //cookie eraser function
createCookie(name,"",-1);
}
createCookie("test", 1, 1);
var mycookie= readCookie("test");
mycookie = parseFloat(mycookie);
console.log(mycookie);
I just found out you just have to disable autosave then hard reset then immediately exit out the tab and if it doesn't work keep trying till it does