How do I load multiple variables from one cookie file?
I have tried this:
if (variable1 != null) {
variable1 = getCookie("variable1");
variable2 = getCookie("variable2");
variable3 = getCookie("variable3");
} else {
variable1 = 0;
variable2 = 0;
variable3 = 0;
}
but that doesn't seem to work.
And this is how the getCookie function looks like:
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);
}
}
}
Related
This question already has answers here:
How do I create and read a value from cookie with javascript?
(23 answers)
Closed 22 days ago.
How to get the value of a cookie in jquery. I want t be able to get the user_first_name and user_last_name from this cookie - {"user_first_name":["Raj Subscriber"],"user_last_name":["Chudasama"]}
function getCookieName(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 "";
}
I tried this:
const userIsLoggedIn = getCookieName('tmm_user_data');
const b = userIsLoggedIn.user_first_name[0];
AND THIS:
const userIsLoggedIn = getCookieName('tmm_user_data');
const b = userIsLoggedIn.user_first_name;
Both return undefined
Just in case you don't know. JS doesn't have a built-in function getCookieName(). You would be required to make it yourself.
Eg:
function getCookie(cname) {
let name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(';');
for(let i = 0; i <ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
Then you can use the function getCookie(<name>) to access the desired one.
Cookie value's does not change after one or two changes or when refreshing the page
it is being used to change the mode (night-light) of a website.
thanks for helping...
window.onload = function (){
changeMode();
}
function setCookie(cname,cvalue,exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires=" + d.toGMTString();
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 "";
}
function changeMode() {
var user=getCookie("user");
if (user != "") {
if(user == "light"){
dark();
setCookie("user", "dark", 30);
}else if(user == "dark"){
light();
setCookie("user", "light", 30);
}
} else {
user = "light";
if (user != "" && user != null) {
setCookie("user", user, 30);
light();
}
}
}
the file:
https://kurddoblaj.com/mode.js
the Result:
https://kurddoblaj.com
I could find what's wrong with my code and miss understanding about how to work with cookies the first problem is I must add another function to remember the browser yes I did something like that but I was changing the mode after refreshing it was because I had had change mode function inside the onload function... the second mistake was I thought that the cookies can be updated if we before the expiration date but it does not so I removed the expiration date to be updated and set again...
you are free to use my script anywhere, thanks for helping....
the code:
window.onload = function (){
rememberMode();
}
function setCookie(cname,cvalue) {
document.cookie = cname + "=" + cvalue;
}
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 "";
}
function rememberMode() {
var user=getCookie("user");
if (user != "") {
if(user == "dark"){
dark();
}else if(user == "light"){
light();
}
} else {
setCookie("user", "light");
light();
}
}
function changeMode() {
var user=getCookie("user");
if(user == "light"){
dark();
setCookie("user", "dark");
}else if(user == "dark"){
light();
setCookie("user", "light");
}
}
function dark() {
document.getElementById("2").style.backgroundColor = "#171717";
document.getElementById("h2").style.color = "#F1F1F1";
document.getElementById("imod").src = "https://kurddoblaj.com/sun.gif";
document.getElementById("search-box").style.backgroundColor = "#2A2A2A";
document.getElementById("search").style.backgroundColor = "#2A2A2A";
document.getElementById("search").style.color = "white";
document.getElementById("z").style.color = "#595959";
document.body.style.backgroundColor = "#171717";
var elems = document.getElementsByClassName('post');
for(var i = 0; i < elems.length; i++) {
elems[i].style.backgroundColor = "#2A2A2A";
elems[i].style.color = "#F1F1F1";
}
}
function light() {
document.getElementById("2").style.backgroundColor = "#E3E3E3";
document.getElementById("h2").style.color = "black";
document.getElementById("imod").src = "https://kurddoblaj.com/moon.gif";
document.getElementById("search-box").style.backgroundColor = "white";
document.getElementById("search").style.backgroundColor = "white";
document.getElementById("search").style.color = "black";
document.getElementById("z").style.color = "black";
document.body.style.backgroundColor = "#E3E3E3";
var elems = document.getElementsByClassName('post');
for(var i = 0; i < elems.length; i++) {
elems[i].style.backgroundColor = "white";
elems[i].style.color = "black";
}
}
IN SHOPIFY STORE: How to add discount code in URL without redirect with javascript. Having a parameter in the url with the discount code.
Every url in your website with the parameter discout will be activated in the checkout.
EXAMPLE OF USE
your discount code is DISCOUNTCODE1
www.myshopifywebsite.com/products/product1?discount=DISCOUNTCODE1
or
www.myshopifywebsite.com?discount=DISCOUNTCODE1
STEP 1
/* Put this in theme.liquid, preferably right before "</body>" inside a script tag */
//this code set the cookie
(function() {
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
var product = urlParams.get('discount');
if (product != null && product.length > 1) {
document.cookie = 'discount=' + product + ';path=/';
}
})();
STEP 2
//Insert this code in cart-template.liquid or cart.liquid at the bottom of the page inside script tag
//Also, make sure your cart's "<form>" has an ID of "cartform".
/*Function to getcookie*/
function getCookie(nome) {
var name = nome + "=";
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() {
var discountCookie = getCookie('discount');
if (discountCookie != null && discountCookie.length > 1) {
document.getElementById('cart_form').action = '/cart?discount=' + discountCookie;
}
})();
STEP 3
//Insert this code in header.liquid (for reciving discount also in a product page), preferably at the bottom of the page inside script tag
//Also, make sure your chechout "<form>" has an ID of "checkoutgsdr".
/*Function to getcookie*/
function getCookie(nome) {
var name = nome + "=";
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() {
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
var product = urlParams.get('discount');
if (product == null || product.length <= 1) {
var discountCookie = getCookie('discount');
if (discountCookie != null && discountCookie.length > 1) {
document.getElementById('checkoutgsdr').action = '/checkout?discount=' + discountCookie;
}
}else{
document.getElementById('checkoutgsdr').action = '/checkout?discount=' + product;
}
})();
I have a program that needs to check for a cookie. When the cookie expires, I want it to display a prompt and alert something.
if (document.cookie == '' || document.cookie == null || document.cookie == undefined) {
var site = prompt("Please enter a valid url:", "http://");
document.cookie = "url=;"
document.cookie = 'expires=Thu, 01 Jan 2030 00:00:00 GMT';
alert("The cookie is expired.");
}
But it doesn't seem to work.
if you search on google you will easily find some basic get/set functions,
Here is a sample link
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 "";
}
then you could use this as below
if(getCookie(nameOfYourCookie) == "")
{
//Show Alert,
//Set cookie
}
function writeCookie()
{
document.cookie="employee=name,status,sal";
document.cookie="dept = name,deptid";
}
Here i set some cookies with "name= value"(Explicitly given)
If i want to access the cookies depending upon "employee" & "dept" then how can i proceed.
I did something like this var x = document.cookie. But it ll fetch all cookies.I want to filter depending upon the key(employee,dept) i set.Can some one tell me how to proceed?
You can use this function ? comes from http://www.w3schools.com/js/js_cookies.asp
var getCookie = function (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 "";
}
You can find a full complete exemple here : http://www.w3schools.com/js/tryit.asp?filename=tryjs_cookie_username
EDIT : I added a function to get the whole cookie as an array getCookieArray. You can check the result in the console log.
var getCookie = function (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 "";
}
var getCookieArray = function (cookieString) {
var subElements = cookieString.split(";");
var subElemPairs = new Array();
var subNameValues = new Array();
for (i = 0; i < subElements.length; i++) {
subElemPairs[i] = subElements[i].split("=");
}
for (i = 0; i < subElemPairs.length; i++) {
subNameValues[subElemPairs[i][0]] = subElemPairs[i][1];
}
return subNameValues;
}
document.cookie="employee=name,status,sal";
document.cookie="dept=name,deptid";
alert(getCookie("employee"));
alert(getCookie("dept"));
console.log(getCookieArray(document.cookie));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>