Hello everyone!
I’ve been working on my clothing website recently. Implementing a newsletter popup and connecting it to MailChimp was somewhat easy.
My problem now is I’m trying to use javascript to set a few cookie rules:
-
If the user exits the modal without signing up five times, it will stop appearing for some time; I think the code has 30 in it.
-
If they have already signed up, the modal hides. I believe this resets after a year.
Here’s my code:
<script>
var closeCountCookieName = 'modalCloseCount'; // Cookie to store close count
var modalClosedCookieName = 'modalClosed'; // Cookie to prevent modal reappearance
var inactivityThreshold = 30 * 24 * 60 * 60; // 30 days in seconds
// Get the close count from the cookie
var closeCount = Cookies.get(closeCountCookieName) || 0; // Default to 0 if cookie doesn't exist
// Get the current timestamp
var currentTime = Math.floor(Date.now() / 1000);
// Check the modal closed cookie (user signed up)
if (Cookies.get(modalClosedCookieName) !== undefined) {
// If it exists, hide the modal
$('.modal-nl').hide(); // Replace '.modal' with your modal element class name
} else {
// Check for close count cookie expiration (inactivity)
var closeCountCookie = Cookies.get(closeCountCookieName);
if (closeCountCookie && currentTime - parseInt(closeCountCookie.split(',')[1]) > inactivityThreshold) {
// Reset close count if cookie is expired (inactive for 30 days)
closeCount = 0;
Cookies.set(closeCountCookieName, closeCount, { expires: 30 }); // Reset and set to expire in 30 days
}
// Show the modal normally (Webflow handles showing the modal)
// Add a click event listener to the modal close button
$('.overlay-brix-close, .close-popup-type, .close-popup-brix’).click(function() {
// Increment the close count
closeCount++;
// Set the close count cookie
Cookies.set(closeCountCookieName, closeCount + ',' + currentTime, { expires: 30 }); // Expires in 30 days and stores timestamp
// Check if close count is 5
if (closeCount >= 5) {
// Set the modal closed cookie to prevent reappearance
Cookies.set(modalClosedCookieName, 'ok', { expires: 365 }); // Expires in a year
// Hide the modal
$('.modal-nl').hide();
}
});
}
</script>
Here are some screenshots. I wasn’t sure if I needed to put them within the designer > Home > little gear (settings) > custom code or the overall Page Settings from within the Dashboard
While trying to test it, I exited the modal, refreshed it five times, and tried signing up. But it would still reappear when I would return.
Please help. Thank you all in advance!
Here is my site Read-Only: LINK
(how to share your site Read-Only link)

