UncleCoder.com

UncleCoder.com

Free programming examples and instructions

Cookies in javascript

Code, Demo for using cookies in javascript.

by Athil


Posted on 22 Mar 2017 Category: Javascript Views: 1328

Edited on 25 Jul 2018


Demo

Cookies are small piece of text that are stored in browser memory. The value remains in the browser when the browser reopens. Here I am going to show how the values are stored in cookies and how it retrieve when the user needs it.

Adding Values into cookie

document.cookie = "cookiename=value";

Taking  all Cookie

var cookie1 = document.cookie;

Taking Specific cookie

var CookieName = "cookiename=";
var DecodedCookie = decodeURIComponent(document.cookie);
var CookieArray = DecodedCookie.split(';');
for(var i = 0; i < CookieArray.length; i++)
{
   var c = CookieArray[i];
   while (c.charAt(0) == ' ')
   {
        c = c.substring(1);
  }
   if (c.indexOf(CookieName) == 0)
   {
        console.log(c.substring(CookieName.length, c.length));
   }

}

Set Expire Timeout of cookie 

document.cookie = "cookiename=value; expires=Thu, 18 Jan 2018 12:00:00 UTC";


Leave a Comment:


Advertisement