I wanted to add an Age Gate / Age Verification pop...
I wanted to add an Age Gate / Age Verification pop…
I wanted to add an Age Gate / Age Verification pop-up for Squarespace. The top search engine result had some code, but it A) didn’t work B) didn’t look very good and C) didn’t have any functionality for tracking cookies. Instead, use this code:
First, put this into the injected code in the Header
<!– Age Verification Pop-up HTML –> <div id=“age-verification-popup”> <div class=“popup-content”> <h2>ARE YOU 21+?<div class=“image-circle-container” style=“margin-bottom: 24px;”> <img src=“https://images.squarespace-cdn.com/content/6788837817ec6330feff09fe/a0416e38-e21f-4f14-88b3-0a8c8589e435/lambi_lamb_black.png” alt=“LAMBI” class=“centered-image” fetchpriority=“high” loading=“eager” decoding=“async” data-loader=“raw”>
Next, inject this CSS:
/* Style for Age Verification Pop-up / #age-verification-popup { display: none; / Initially hidden, JS will show it if needed / position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.8); / Semi-transparent black overlay / z-index: 9999; font-family: Arial, sans-serif; / Or choose a font that matches / } .popup-content { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); background-color: #fff; / White background / padding: 40px 30px; / Adjust padding as needed / text-align: center; border: 1px solid #ccc; / Optional: adds a light border / min-width: 300px; / Minimum width / box-shadow: 0 4px 8px rgba(0,0,0,0.1); / Optional subtle shadow / } / Optional logo styling / / .popup-logo { max-width: 80px; margin-bottom: 20px; } / .popup-content h2 { font-size: 1.2em; / Adjust size as needed / margin-top: 0; margin-bottom: 25px; font-weight: bold; line-height: 1.4; color: #000; / Black text / } .button-container { margin-bottom: 25px; } #verify-button-yes, #verify-button-no { background-color: #000; / Black background / color: #fff; / White text / padding: 12px 30px; / Adjust padding / border: none; cursor: pointer; font-size: 1em; font-weight: bold; margin: 0 5px; / Space between buttons / min-width: 80px; / Minimum width for buttons / transition: background-color 0.2s ease; / Smooth hover effect / } #verify-button-yes:hover, #verify-button-no:hover { background-color: #333; / Slightly lighter black on hover / } .popup-content .disclaimer { font-size: 0.9em; / Smaller font size for disclaimer / color: #555; / Grey text / line-height: 1.5; } .image-circle-container { width: 70px; / Adjust size of the circle as needed / height: 70px; / Must be the same as width for a perfect circle / background-color: transparent; / Black background / border-radius: 50%; / This makes the div circular / display: flex; / Enables flexbox for easy centering / justify-content: center; / Centers content (image) horizontally / align-items: center; / Centers content (image) vertically / overflow: hidden; / Ensures image doesn’t spill outside the circle if it’s too big / margin-left: auto; / Centers the circle container itself horizontally / margin-right: auto; / Centers the circle container itself horizontally / } .centered-image { display: block; / Removes extra space below the image / height: 50px; / Your desired image height / width: auto; / Let width adjust automatically to maintain aspect ratio / max-width: 90%; / Optional: Prevents image from touching the circle edge / max-height: 90%;/ Optional: Prevents image from touching the circle edge */ }
Finally, inject this code into the footer
<script> // JavaScript for Age Verification Pop-up with Cookies document.addEventListener(“DOMContentLoaded”, function () { const popup = document.getElementById(“age-verification-popup”); const verifyButtonYes = document.getElementById(“verify-button-yes”); const verifyButtonNo = document.getElementById(“verify-button-no”); const cookieName = “ageVerified”; // Name of our cookie // Function to set a cookie function setCookie(name, value, days) { let expires = “”; if (days) { const date = new Date(); date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); expires = “; expires=” + date.toUTCString(); } // Add SameSite=Lax and Secure attributes for better security, especially if using HTTPS // For local testing (http), you might omit ‘Secure’ // document.cookie = name + “=” + (value || “”) + expires + “; path=/; SameSite=Lax; Secure”; document.cookie = name + “=” + (value || “”) + expires + “; path=/; SameSite=Lax”; // Use this line if not using HTTPS for testing } // Function to get a cookie function getCookie(name) { const nameEQ = name + “=”; const ca = document.cookie.split(’;’); for(let i = 0; i < ca.length; i++) { let c = ca[i]; while (c.charAt(0) === ’ ’) c = c.substring(1, c.length); if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length); } return null; } // Check if the age verification cookie exists const isVerified = getCookie(cookieName); if (!isVerified) { // If the cookie doesn’t exist, show the pop-up if (popup) { popup.style.display = “block”; } } else { // If the cookie exists, keep the pop-up hidden (it’s hidden by default CSS) console.log(“Age already verified.”); } // Event listener for the “YES” button if (verifyButtonYes) { verifyButtonYes.addEventListener(“click”, function () { // Set a cookie to remember verification for 365 days setCookie(cookieName, “yes”, 365); // Hide the pop-up if (popup) { popup.style.display = “none”; } }); } // Event listener for the “NO” button if (verifyButtonNo) { verifyButtonNo.addEventListener(“click”, function () { // Redirect the user to the specified URL when they click “NO” window.location.href = “https://www.youtube.com/watch?v=s50vvwTystA”; }); } });
Enjoy!