Creating a cookie alert message with a closing button on your homepage in Webflow is a crucial step for user consent management. Here's how you can set it up:
1. Add a Popup Modal for the Cookie Alert
- Create a new Section within your homepage to hold the cookie alert message.
- Add a Div Block inside this section to serve as the container for your alert.
- Ensure it is positioned fixed at the bottom of the viewport for better visibility.
2. Design the Alert Message
- Insert a Text Block inside the Div Block with your cookie consent message.
- Style this block with a background color, padding, and font settings to ensure it's clear and readable.
3. Add a Closing Button
- Place a Button within the same Div Block, next to the text.
- Label it clearly, such as “Accept Cookies” or simply “Close”.
- Style this button to match the overall design of your site.
4. Implement Interactions
- Go to the Interactions panel within Webflow.
- Create a new Click Trigger interaction on the button.
- Set the interaction to hide the cookie alert Div Block when the button is clicked.
5. Add Custom Code for Persistence
- Since Webflow's interactions are session-based, add custom code to remember the user’s choice using cookies.
- Go to the Project Settings > Custom Code tab.
- In the Footer Code section, include custom JavaScript to set a cookie when the button is clicked. Example:
document.querySelector('.close-button-class').addEventListener('click', function() {
document.querySelector('.cookie-alert-class').style.display = 'none';
document.cookie = "cookiesAccepted=true; expires=Fri, 31 Dec 9999 23:59:59 GMT";
});
6. Check the Cookie on Page Load
- Add a small script to check if the cookie exists and set the display of the alert accordingly.
- Include this script in the same Custom Code section:
if (document.cookie.indexOf('cookiesAccepted=true') === -1) {
document.querySelector('.cookie-alert-class').style.display = 'block';
} else {
document.querySelector('.cookie-alert-class').style.display = 'none';
}
Summary
To create a cookie alert message with a closing button, design a fixed position popup on your homepage, add a button to dismiss it, and use Webflow interactions combined with custom JavaScript to maintain persisting user consent. Remember to test your setup by clearing browser cookies to ensure that it functions correctly.