How to Set, Get and Delete Cookies using Vanilla JavaScript

How to Set, Get and Delete Cookies using Vanilla JavaScript
In this tutorial, we are going to learn how to SET, GET and DELETE Cookies using Vanilla JavaScript. We will create functions to reduce our codes and make it easy to do these operations. It will help you in your JavaScript projects. Getting Started Follow the following steps to use the codes in your HTML document. Table of Contents Basic Add the following JavaScript codes under a <script> in the head section of your HTML document. /** * cookie by Fineshop Design * License: MIT */ const cookie=Object.defineProperties({},{get:{value(a){if("string"==typeof a){const b=this.cookie.match(new RegExp(`(?:^|; )${a.replace(/([.$?*|{}()[\]\\/+^])/g,"$1")}=([^;]*)`));if(b&&"string"==typeof b[1])return decodeURIComponent(b[1])}return null}},getAll:{value(){const a={},b=this.cookie.split("; ");for(let c=0;c<b.length;c+=1){const[d,e]=b[c].split("=");d&&(a[d]=decodeURIComponent(e||""))}return a}},has:{value(a){return nu…

About the author

~ Hello World!

Post a Comment