Edrol97
Silver Coder
Hi all,
I've managed to make the attached cookie box, but can't seem to get it to work. I think I got the beginner code from W3Schools. Here's the JS. How do I make this work? It should appear on the homepage of my site, and when closed stay on that page. How would I do this?
I've managed to make the attached cookie box, but can't seem to get it to work. I think I got the beginner code from W3Schools. Here's the JS. How do I make this work? It should appear on the homepage of my site, and when closed stay on that page. How would I do this?
JavaScript:
const consentBox = document.getElementById("consentBox");
const acceptBtn = document.querySelector(".consentButton");
const rejectBtn = document.querySelector(".rejectButton");
acceptBtn.onclick = () => {
document.cookie = "CookieBy=HouseofLord; max-age=" + 60 * 60 * 24;
if (document.cookie) {
consentBox.classList.add("hide");
} else {
alert(
"Cookie can't be set! Please" +
" unblock this site from the cookie" +
" setting of your browser."
);
}
};
rejectBtn.onclick = () => {
alert("Cookies rejected. Some functionality may be limited.");
consentBox.classList.add("hide");
};
let checkCookie = document.cookie.indexOf("CookieBy=HouseofLord");
checkCookie !== -1
? consentBox.classList.add("hide")
: consentBox.classList.remove("hide");