-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookier.js
45 lines (39 loc) · 1.33 KB
/
cookier.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
* Cookie alert by Decaded
* https://github.com/Decaded/Cookier
* Released under MIT license
*/
// Function to set a cookie
function setCookie(name, value, days) {
const date = new Date();
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); // Expiry time
const expires = 'expires=' + date.toUTCString();
document.cookie = name + '=' + value + ';' + expires + ';path=/';
}
// Function to get a cookie by name
function getCookie(name) {
const cookies = document.cookie.split(';');
for (let cookie of cookies) {
cookie = cookie.trim();
if (cookie.indexOf(name + '=') === 0) {
return cookie.substring((name + '=').length, cookie.length);
}
}
return null;
}
// Function to show the cookie banner
function showCookieBanner() {
const banner = document.getElementById('cookierBanner');
banner.classList.add('show');
}
// Check if the cookie is set
const cookieName = 'Cookier';
if (!getCookie(cookieName)) {
showCookieBanner();
}
// Handle click on "Close (X)" button
document.getElementById('closeCookierButton').addEventListener('click', function () {
// alert('Good Boy, have a cookie for following directions: 🍪'); // Uncomment this to display message when user click the 'X' button
setCookie(cookieName, 'true', 365); // Set cookie for 1 year
document.getElementById('cookierBanner').classList.remove('show');
});