How to manage Cookies in the Browser via Javascript?

About

This article is about HTTP cookies management in the client side (browser) via javascript.

Cookie are one way to store data in the browser.

document.cookie is a property of the browser document that returns the HTTP cookies that apply to the Document. If no cookies or cookies can't be applied to this resource, the empty string is returned.

With the Web API and Document cookie property

Management

DevTool

You can browse the cookies with the devtool

Cookie Devtool

or an browser extension. Example: Chrome extension Edit this cookie

HttpOnly

A cookie set with the HttpOnly flag, tells the browser that Javascript cannot access this cookie (ie the cookie should only be managed by the server and set in the browser request)

Set

Set two cookies

document.cookie = "name=nico";
document.cookie = "favorite_color=blue";

Get

All

  • Read the cookies. The cookies are in a string separated with a ;
console.log("All the cookies:");
console.log(document.cookie);

For the value of the google analytics cookie, see Google Analytics - Utma Cookie

Filter

  • Only the cookie that starts with an underscore
console.log("All the cookies:");
console.log(document.cookie.split("; ").filter( (item) => item.trim().startsWith('_') ));

by name

There is no function in the Javascript Web API to get a cookie by name.

We need to parse the value of document.cookie. This is a string of all cookies in a key pair format separated by a ;

  • Setting a cookie
cookie_key = "name";
document.cookie = cookie_key+"=nico";
  • Regular expression method. Because the key value is saved in variable, we cannot create a regular expression pattern as a string and need to use a regexp constructor
regexp = new RegExp("(?:(?:^|.*;\\s*)"+cookie_key+"\\s*\\=\\s*([^;]*).*$)|^.*$");
var cookieValue = document.cookie.replace(regexp, "$1");
console.log("Regular expression    : The value of the cookie with the name key is ("+cookieValue+")");
var cookieValue = document.cookie
    .split(";")
    .filter( (item) => item.trim().startsWith(cookie_key) )[0]
    .split("=")[1];
console.log("Functional Programming: The value of the cookie with the name key is ("+cookieValue+")");
  • Result

Delete

To delete a cookie, just set the expiration date in the past.

document.cookie = "key=; expires=Thu, 01 Jan 1970 00:00:00 GMT";

Security Consideration

Library

Example:

import Cookies from "js-cookie";
Cookies.get('cookie-name');





Discover More
Jwt Auth Flow
Authentication - Jwt (Json web token)

json web token is a token. It's also known as jot. When a JWT is signed, it becomes a JWS and can be used for sender authentication and authorization. The main purpose of JWTs is to transfer (ie identity...
Browser - Storage (Client Side Data)

This page is client side data (ie stateless session) in the browser. cookie local storage WebStorage - name/value pairs - Method of storing data locally...
DOM - Document (Object)

Every XML doc and HTML document (Web page) in an HTML UA is represented by a TR/html5/dom.htmlDocument object. A document in the context of a browser is generally a HTML document (Web Page). The Document...
Card Puncher Data Processing
Google Analytics - Cookies

used in google analytics Their prefixed are called utm because of . Google analytics comes fro Urchin. ... ...
HTML - Browsing context

A browsing context is a navigational context (environment) in which HTML document are rendered to the user. Each browsing context has: its own variable its own cookie its own dom and eventually...
Javascript - Functional Programming

in javascript. array operationsDoc To get the cookies All function returns an array. A few returns an element...
Browser
The devtool is a set of web developer tools embedded in every browser

This article shows briefly what they can do and how to access it
Set Cookie Block Bad Domain Att Vs Current Host Url
The domain property of a cookie in depth

This article is about the domain property of a cookie and defines what is a domain, how it's used and what's permitted.
Chrome Cookies
What is a Cookie? (HTTP Set-Cookie Header )

A cookie is: a key-value data with some associated that control how the browser should manage them. set by a HTTP response via the set-cookie header The received cookies by the browser can be...



Share this page:
Follow us:
Task Runner