• Skip to primary navigation
  • Skip to main content
  • Skip to footer

Merchant Labs

Helping online merchants succeed is what we do best!

  • About
  • Contact
  • My Account

Experiments / May 9, 2017

Native JavaScript Cookie Functions

Set A Cookie

function setCookie(cname, cvalue, exdays) {
if (!cname) return;
if (exdays){
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = “expires=”+ d.toUTCString()+”;”;
} else {
var expires = “”;
}
document.cookie = cname + “=” + cvalue + “;” + expires + “path=/”;
}

Delete A Cookie

document.cookie = “username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;”;

Get A Cookie

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

Credit: https://www.w3schools.com/js/js_cookies.asp

Filed Under: Experiments Tagged With: Coding, Javascript

Reader Interactions

Leave a Reply Cancel reply

You must be logged in to post a comment.

Footer

Shopify Partner

Let Merchant Labs, with over 15 years experience in ecommerce, help you SUCCEED online! From …

Read More about Shopify Development Services

  • Twitter
  • YouTube

Ajax-Cart Coming Soon

What is Ajax-cart for Shopify? Ajax-cart for Shopify is a big project to bring some of the most wanted features to the Shopify cart page. This page is also known as the basket screen. Its …

more about Ajax-Cart for Shopify

Tags

Apps Script Coding Documentation Javascript MySQL PHP Shopify Liquid

Contact Us

Merchant Labs
Los Angeles, California

P: 424-236-0372
E: [email protected]

Proud Partners For

Copyright © 2025 · Digital Pro on Genesis Framework · WordPress · Log in