You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
JavaScript can create, read, and delete cookies with the document.cookie property.
Without Expire Time Browser will forget Cookie after reload or close browser. You can also add an expiry date (in UTC time).
If set path to root path=/ will be available everywhere inside application. By default, the cookie belongs to the root.
If set path to specific URL like path=/about will be available only inside that url.
Default Way:
document.cookie="username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";
Using Function:
/** * @description - Function Helps to Set Cookie. * @param {String} Key of Cookie. * @param {String} Value of Cookie. * @param {Number} Days of Duration of Cookie. Default `1` Day * @param {String} Path of Cookie. Default `path=/` * @returns {Object} Created Cookie In Object Format. */exportconstsetCookie=(key,value,days=1,path='/')=>{// Make Cookie StringconstcookieString=`${key}=${value};expires=${newDate(newDate().getTime()+days*24*60*60*1000).toUTCString()};path=${path}`;// Create Cookiedocument.cookie=cookieString;// Create Object From CookieconstcookieObject=document.cookie.split(';').map((v)=>v.trim().split('=')).reduce((state,[k,v])=>({ ...state,[k]: v}),{});returncookieObject;};
Get a Cookie Values:
document.cookie will return all cookies in one string much like: cookie1=value; cookie2=value; cookie3=value;
Default Way:
document.cookie
Using Function:
/** * @description - This Function helps Get Single or multiple Cookie Values. * @param {String} Key of Cookie. Default Nothing. * @returns {String || Object} If Key provide Return Valu of that key otherwise full Object. */exportconstgetCookie=(key=false)=>{// Make Cookie String To ObjectconstcookieObject=document.cookie.split(';').map((v)=>v.trim().split('=')).reduce((state,[k,v])=>({ ...state,[k]: v}),{});returnkey ? cookieObject[key] : cookieObject;};
Check a Cookie Value:
No method available by default to check cookie Value is available or not.
Using Function:
/** * @description - This Function helps Check Cookie Value Available or Not. * @param {String} Key of Cookie. * @returns {Boolean} Boolean value cookie exists or not. */exportconstcheckCookie=(key)=>{// Make Cookie String To ObjectconstcookieObject=document.cookie.split(';').map((v)=>v.trim().split('=')).reduce((state,[k,v])=>({ ...state,[k]: v}),{});returnkey ? (cookieObject[key] ? true : false) : false;};
Change or modify a Cookie:
With JavaScript, you can change a cookie the same way as you create it.
The old cookie is overwritten.
Default Way:
document.cookie="username=John Smith; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";
Using Function:
/** * @description - Function Helps to Set Cookie. * @param {String} Key of Cookie. * @param {String} Value of Cookie. * @param {Number} Days of Duration of Cookie. Default `1` Day * @param {String} Path of Cookie. Default `path=/` * @returns {Object} Created Cookie In Object Format. */exportconstsetCookie=(key,value,days=1,path='/')=>{// Make Cookie StringconstcookieString=`${key}=${value};expires=${newDate(newDate().getTime()+days*24*60*60*1000).toUTCString()};path=${path}`;// Create Cookiedocument.cookie=cookieString;// Create Object From CookieconstcookieObject=document.cookie.split(';').map((v)=>v.trim().split('=')).reduce((state,[k,v])=>({ ...state,[k]: v}),{});returncookieObject;};
Delete a Cookie:
Deleting a cookie is very simple.
You don't have to specify a cookie value when you delete a cookie.
Just set the expires parameter to a past date.
Default Way:
document.cookie="username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
Using Function:
/** * @description - Function Helps to Remove Cookie. * @param {String} Key of Removed Item of Cookie. * @param {String} Path of Removed Cookie. Default `path=/` * @returns {Object} Exists Available Cookie As Object. */exportconstremoveCookie=(key,path='/')=>{// Make Removed Cookie StringconstcookieString=`${key}=;expires=${newDate(0).toUTCString()};path=${path}`;// Set Emptied Cookie on Previous Datedocument.cookie=cookieString;// Create Object From CookieconstcookieObject=document.cookie.split(';').map((v)=>v.trim().split('=')).reduce((state,[k,v])=>({ ...state,[k]: v}),{});returncookieObject;};