JS Cookies

Overview

There are a plethora of cookie scripts on the web, each offering similar functionality. Over the years I have built up a short cookie library that I use whenever the need arises. It offers a couple of extra functions that are sometimes useful, although you don’t always find them in other libraries.

This standard function has two required variables, and has four other optional variables

1
2
// set a cookie
cookie.set("cookieName", "cookieValue");
  • name: the name of the new cookie (required)
  • value: the value of the new cookie (required)
  • expires: the number of days in which the cookie should expire - if not set, cookies will be session cookies only
  • path: the local path of the page that the cookie relates to
  • domain: the domain the cookie is being set from
  • secure: whether the cookie should only be available for https

This function has a single variable - the name of the cookie in question. If the requested cookie is not found, a false (null) value is returned.

1
2
// get a cookie
var cookieVal = cookie.get("cookieName");

Get all cookies

This returns an array of all cookies, that can then be iterated over.

1
2
// get all cookies
var cookieArray = cookie.getAll();

## Remove cookie

This function again has one or more variables. The first is the name of the cookie in question. It does not check to see if the cookie in question exists. It is also possible to specify an optional path and domain.

1
2
// remove a cookie
cookie.remove("cookieName");

Remove all cookies

A short function with no parameters that removes all cookies for the site in question.

1
2
// remove all cookies
cookie.removeAll();

Count cookies

A surprisingly useful function that allows you to count how many cookies have been set.

1
2
// count cookies
var cookieCount = cookies.count();