How to Add a Cookie Alert using Bootstrap Cookie Alert

May 24, 2022 | Programming

If you’re looking to add a simple yet effective cookie alert to your website utilizing Bootstrap 4 or 5, you’re in the right place! This guide will help you integrate the Bootstrap Cookie Alert into your project without any dependencies, ensuring that your visitors are informed about cookie usage while enhancing user experience.

Step-by-Step Guide

Follow these steps to successfully implement the Cookie Alert:

Step 1: Include CSS in Your Head

To start, you need to include the CSS file for the cookie alert. Place the following line in the <head> section of your HTML document, ensuring it comes after your Bootstrap CSS:

<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Wruczek/Bootstrap-Cookie-Alert@gh-pages/cookiealert.css">

Step 2: Add the HTML Markup

Next, insert the following HTML markup into your page. This markup contains the cookie alert message:

<!-- START Bootstrap-Cookie-Alert -->
<div class="alert text-center cookiealert" role="alert">
    <b>Do you like cookies?</b> 🍪 We use cookies to ensure you get the best experience on our website. 
    <a href="https://cookiesandyou.com" target="_blank">Learn more</a>
    <button type="button" class="btn btn-primary btn-sm acceptcookies">I agree</button>
</div>
<!-- END Bootstrap-Cookie-Alert -->

Step 3: Include the JavaScript

To make the cookie alert functional, you need to include the following JavaScript just after the HTML markup:

<script src="https://cdn.jsdelivr.net/gh/Wruczek/Bootstrap-Cookie-Alert@gh-pages/cookiealert.js"></script>

Understanding the Functionality

Think of the cookie alert as your friendly doorman. Whenever someone arrives at your website, the doorman greets them with a message informing them about cookies—similar to how a doorman might inform guests about the house rules. Just as the doorman allows guests to enter once they acknowledge the house rules by nodding, the cookie alert provides an option for users to accept cookies before they move freely through your site. This way, both parties can enjoy the experience without misunderstandings!

Handling the Accept Event

If you want to trigger an action once a user accepts the cookies, you can listen for the cookieAlertAccept event using the following JavaScript snippet:

window.addEventListener('cookieAlertAccept', function() {
    alert('Cookies accepted');
});

Troubleshooting Ideas

  • If the cookie alert does not appear, double-check your CSS and JavaScript file paths to ensure they are correct.
  • Ensure that you have included the CSS file after your Bootstrap stylesheet to avoid any conflicts.
  • If the alert appears but the click function does not work, validate that the acceptcookies button class is intact without typos.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

By following these simple steps, you can effectively inform your visitors about cookie usage in a user-friendly manner. At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Resources

You can check out the live demos for Bootstrap 5 and 4 to see how the cookie alert functions:

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox