Munna Bhakta
Munna Bhakta Having more than 10 years of experiance in Web Development, GIS Development, Desktop Development

Cookies read and write with JavaScript Only

Cookies read and write with JavaScript Only

JavaScript has everthing what you need to perform any task in browser and easy to use as comparision to other libraries which is developed on JavaScript.So Why should we go to download and use other libraries?

Wrote this blog because I don’t want to use heavy plugin/library for simple task which can slow down performance of my application.

Using multiple JavaScript library for small task will down performance of your Application so always try to write your small logics to perform tiny task.

1
2
3
4
5
6
7
8
9
document.cookie = 'firstname=Munna;lastname=Bhakta'//Will Never expire this cookie until user remove.

//Write simple function to reuse anywhere in application.
function setCookie(cname, cvalue, exdays) {
  var d = new Date();
  d.setTime(d.getTime() + (exdays*24*60*60*1000));
  var expires = "expires="+ d.toUTCString();
  document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

Good thing about cookie is “it never duplicate key” so when you will provide new value it will update existing cookie. No need additional code. For example.

1
document.cookie = 'firstname=Naitik;lastname=Raj'// Will add cookie firstName and lastName.

Will return all cookie value as string

1
document.cookie 
1
2
3
4
function getCookie(key){
    return document.cookie.split(';').map(cookie=> {return cookie.indexOf(key)>-1?cookie.split("=")[1]:null }).join("");
}
readCookie('firstName');// Will Return value of firstName

Details about used functions

  1. split - Will make array of string by splitting ‘;’ saperator.
  2. map - Will loop over all array item returned by split function.
  3. indexOf - compare key of each item in array and return >-1 if found else will return -1.
  4. join - Will join array to string.

You don’t need to provide cooke value if you want to remove. For example.

document.cookie='firstName=;expires=Thu, 01 Jan 1970 00:00:00 UTC;'

Here is the complete JavaScript file which you can download and use in your application or you can write your own program by taking some help.