How to Set & Read Javascript Cookies

by Rob Johnson in on 5 Sep 2015

tl;dr:A few pointers on how to set and read javascript cookies.

Here are a few tricks with javascript that can be adapted for many uses. That said, the users browser will need to have javascript enabled. The following code doesn't require jQuery.

Step 1: Create a cookie

document.cookie="cookiename=cookievalue";

Step 2: Initial script to allow you to read cookies

var cookies;

function readCookie(name,c,C,i){
  if(cookies){ return cookies[name]; }

  c = document.cookie.split('; ');
  cookies = {};

  for(i=c.length-1; i>=0; i--){
    C = c[i].split('=');
    cookies[C[0]] = C[1];
  }

  return cookies[name];
}

window.readCookie = readCookie; // or expose it however you want

Step 3: Request the value of a specific cookie

readCookie("cookiename");

code

Was this article helpful to you? /