Skip to content

Instantly share code, notes, and snippets.

@Ebrahim-Ramadan
Created August 9, 2024 19:11
Show Gist options
  • Save Ebrahim-Ramadan/2ef46144cece7848c06c69789a37e6d8 to your computer and use it in GitHub Desktop.
Save Ebrahim-Ramadan/2ef46144cece7848c06c69789a37e6d8 to your computer and use it in GitHub Desktop.
just a simple implementation for js-cookie alternative
// Function to get a cookie
function getCookie(name) {
const nameEQ = name + "=";
const ca = document.cookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) === 0) return JSON.parse(c.substring(nameEQ.length, c.length));
}
return null;
}
// Function to set a cookie
export function setCookie(name, value, days) {
const expires = new Date();
expires.setTime(expires.getTime() + days * 24 * 60 * 60 * 1000);
document.cookie = name + '=' + JSON.stringify(value) + ';expires=' + expires.toUTCString() + ';path=/';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment