🍪 Cookie Consent Handler (cookies.js)
This script manages the cookie consent prompt on the logistics site. It tracks user acceptance, sets a persistent cookie, and toggles the UI accordingly. It ensures compliance with basic cookie regulations and enhances user experience.
DOM References
This section lists the primary elements the script interacts with.
| Variable | Selector | Description |
|---|---|---|
| consentBox | #consentBox |
Container for the consent prompt |
| acceptBtn | .consentButton |
Button to accept all cookies |
| rejectBtn | .rejectButton |
Button to reject all cookies |
Cookie Configuration
The script uses a single cookie to remember consent.
| Attribute | Value | Purpose |
|---|---|---|
| Name | CookieBy |
Tracks acceptance state |
| Value | accept |
Indicates user granted consent |
| Max-Age | 2592000 (30 days) |
Duration before expiration |
| SameSite | Lax |
Restricts cross-site sending |
| Secure | (flag present) | Sends only over HTTPS |
Event Handlers
✅ Accept Button
This handler fires when users click Allow. It sets the consent cookie and hides the prompt.
acceptBtn.onclick = () => {
try {
document.cookie = "CookieBy=accept; max-age=2592000; SameSite=Lax; Secure";
consentBox.classList.add("hide");
} catch (e) {
console.error("Failed to set cookie:", e);
consentBox.classList.add("hide");
}
};
(See code snippet )
❌ Reject Button
This handler fires when users click Decline. It hides the prompt without storing a preference.
rejectBtn.onclick = () => {
consentBox.classList.add("hide");
};
(See code snippet )
Initialization Logic
On script load, the code checks for an existing consent cookie. If found, it immediately hides the prompt. Errors in reading cookies log to the console.
try {
if (document.cookie && document.cookie.indexOf("CookieBy=accept") !== -1) {
consentBox.classList.add("hide");
}
} catch (e) {
console.error("Failed to read cookie:", e);
}
(See code snippet )
Integration in HTML
Include the consent prompt container and load the script at the end of your body.
<div id="consentBox" class="slideInUp">
<p>We use cookies to enhance your experience on our website...</p>
<div class="text-center">
<button class="btn btn-primary mt-4 me-3 consentButton">Allow</button>
<button class="btn btn-danger mt-4 rejectButton">Decline</button>
</div>
</div>
<script src="js/cookies.js" type="text/javascript"></script>
(Example from page template )
Flowchart of Logic
flowchart TD
A[Page Load] --> B{CookieBy=accept?}
B -- Yes --> C[Hide Consent Box]
B -- No --> D[Show Consent Box]
D --> E[User Click]
E -->|✅ Accept| F[Set CookieBy=accept]
F --> C
E -->|❌ Reject| C
This diagram shows the user flow from page load to final state. Ensure your CSS defines the .hide class (e.g., display: none;) to complete the hiding behavior.