Last active
August 20, 2022 05:29
-
-
Save secretorange/64c766062e6540302d1c8fb714222d6d to your computer and use it in GitHub Desktop.
Simple Cookie Consent banner in React without dependencies.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function setCookie(cname: string, cvalue: string, exdays: number) { | |
const d = new Date(); | |
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000)); | |
let expires = "expires="+d.toUTCString(); | |
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/"; | |
} | |
export function getCookie(cname: string) { | |
let name = cname + "="; | |
let ca = document.cookie.split(';'); | |
for(let i = 0; i < ca.length; i++) { | |
let c = ca[i]; | |
while (c.charAt(0) == ' ') { | |
c = c.substring(1); | |
} | |
if (c.indexOf(name) == 0) { | |
return c.substring(name.length, c.length); | |
} | |
} | |
return ""; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment