-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoockieConsent.js
81 lines (68 loc) · 2.9 KB
/
coockieConsent.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
document.addEventListener('DOMContentLoaded', function () {
// Function to set a cookie with a specified path
function setCookie(name, value, days) {
const date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
const expires = "expires=" + date.toUTCString();
document.cookie = name + "=" + value + ";" + expires + ";path=/"; // Set path to root
}
// Function to get a cookie by name
function getCookie(name) {
const value = "; " + document.cookie;
const parts = value.split("; " + name + "=");
if (parts.length === 2) return parts.pop().split(";").shift();
}
// Function to delete a cookie by name
function deleteCookie(name) {
document.cookie = name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/"; // Set path to root
}
// Function to check for cookie consent
function checkCookieConsent() {
const consent = getCookie("cookieConsent");
const sessionDecline = sessionStorage.getItem("cookieConsentDeclined");
if (!consent && !sessionDecline) {
// Show the cookie consent modal if consent is not found and not declined in the current session
openCookieConsentModal();
}
}
// Function to show the cookie consent modal
function openCookieConsentModal() {
const cookieModal = document.getElementById('cookieConsentModal');
if (cookieModal) {
cookieModal.classList.add('active');
document.body.classList.add('modal-open');
}
}
// Function to accept cookies and close the modal
function acceptCookies() {
setCookie("cookieConsent", "true", 365); // Store consent for 1 year
const cookieModal = document.getElementById('cookieConsentModal');
if (cookieModal) {
cookieModal.classList.remove('active');
document.body.classList.remove('modal-open');
}
}
// Function to decline cookies, delete all cookies and close the modal
function declineCookies() {
// Mark that the user declined cookies for this session
sessionStorage.setItem("cookieConsentDeclined", "true");
// List of cookies to delete
const cookiesToDelete = ['userCurrencySettings', 'cookieConsent'];
// Delete each cookie
cookiesToDelete.forEach(cookieName => {
deleteCookie(cookieName);
});
// Clear localStorage as well
localStorage.clear();
const cookieModal = document.getElementById('cookieConsentModal');
if (cookieModal) {
cookieModal.classList.remove('active');
document.body.classList.remove('modal-open');
}
}
// Check consent on page load
checkCookieConsent();
// Attach global functions to the window object if needed
window.acceptCookies = acceptCookies;
window.declineCookies = declineCookies;
});