Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      June 1, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      June 1, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      June 1, 2025

      How To Prevent WordPress SQL Injection Attacks

      June 1, 2025

      7 MagSafe accessories that I recommend every iPhone user should have

      June 1, 2025

      I replaced my Kindle with an iPad Mini as my ebook reader – 8 reasons why I don’t regret it

      June 1, 2025

      Windows 11 version 25H2: Everything you need to know about Microsoft’s next OS release

      May 31, 2025

      Elden Ring Nightreign already has a duos Seamless Co-op mod from the creator of the beloved original, and it’ll be “expanded on in the future”

      May 31, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Student Record Android App using SQLite

      June 1, 2025
      Recent

      Student Record Android App using SQLite

      June 1, 2025

      When Array uses less memory than Uint8Array (in V8)

      June 1, 2025

      Laravel 12 Starter Kits: Definite Guide Which to Choose

      June 1, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Photobooth is photobooth software for the Raspberry Pi and PC

      June 1, 2025
      Recent

      Photobooth is photobooth software for the Raspberry Pi and PC

      June 1, 2025

      Le notizie minori del mondo GNU/Linux e dintorni della settimana nr 22/2025

      June 1, 2025

      Rilasciata PorteuX 2.1: Novità e Approfondimenti sulla Distribuzione GNU/Linux Portatile Basata su Slackware

      June 1, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Implementing a Cookie Banner with JavaScript: A Simple Guide

    Implementing a Cookie Banner with JavaScript: A Simple Guide

    January 31, 2025

    In this article, we will learn how to store cookies using a cookie consent feature, utilizing HTML, CSS, and JavaScript.

    Cookies are small pieces of data stored on a user’s device during their visit to a website. They are used to store information, which helps in loading data more quickly and improving the overall user experience. Cookies also play a role in managing user sessions, allowing users to stay logged in as they navigate through different pages on the same site.

    To check where cookies are stored, follow these steps:

    1. Right-click on the page and select the “Inspect” option from the menu.
    2. Navigate to the “Application” tab.
    3. In the “Storage” section, click on “Cookies.”
    4. Open the “Cookies” section to view all the cookies stored by the site.

     

    Preview:

    Cookie Stored

     

    You can also manage the cookies here, such as editing, deleting, or storing them.

    This is a simple implementation of a modal pop-up that informs users about cookies and tracks whether they have dismissed it. It uses HTML, CSS, and JavaScript. The modal is displayed when a button is clicked, and once the user dismisses it, a cookie is set to remember their action, preventing the modal from showing again on subsequent visits.

     

    Step 1: index.html (Structure of the Page)

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link href="./index.css" rel="stylesheet">
        <title>Cookies Modal Pop Up</title>
    </head>
    <body>
        <h2>Cookies Modal Pop Up</h2>
        <button class="myBtn">Click Open To Modal</button>
        <div class="modal">
            <div class="modal-content">
                <span class="close">&times;</span>
                <p>This websites uses cookies to ensure you get the best experience on the websites.</p>
                <a href="https://www.office.com/?auth=2" class="modal-content-link">Learn More</a>
            </div>
        </div>
    <script src="index.js"></script>
    </body>
    </html>
    

    This code provides the structure of the modal pop-up. It contains a button (Click Open To Modal) that opens the modal, which informs the user about cookies. The modal includes a close button (×) to close it.

     

    Step 2: index.js (JavaScript Logic)

    let modal = document.querySelector(".modal");
    let clear = document.querySelector(".close");
    let btn = document.querySelector(".myBtn");
    
    function checkCookie() {
        var cookies = document.cookie.split(";");
        for (var i = 0; i < cookies.length; i++) {
            var cookie = cookies[i];
            if (cookie.indexOf("dismissed=") === 0) {
                return true;
            }
        }
    }
    function setCookie() {
        document.cookie = "dismissed=true;";
    }
    btn.addEventListener('click', function () {
        if (!checkCookie()) {
            modal.style.display = "block";
        }
    });
    clear.addEventListener('click', function () {
        modal.style.display = "none";
        setCookie();
    });
    

    Below is the explanation of the index.js code provided above:

    Selectors:

    • modal: Selects the modal element that displays the cookie warning.
    • clear: Selects the close button that hides the modal.
    • btn: Selects the button that activates the modal to open.

    Functions:

    • checkCookie(): This function checks if the user has already dismissed the modal by looking for a cookie named dismissed. If this cookie is found, it returns true, meaning the modal won’t display again.
    • setCookie(): This function sets a cookie named dismissed with a value of true when the user closes the modal. This ensures that the modal will not appear again on the user’s next visit.

    Event Listeners:

    • addEventListener(‘click’, …): When the button is clicked, it checks if the dismissed cookie exists. If not, the modal will be displayed.
    • addEventListener(‘click’, …): When the user clicks the close button (&times;), the modal is hidden, and the dismissed cookie is set, ensuring the modal doesn’t show again during future visits.

     

    Step 3: index.css (Styling the Page with CSS)

    In the index.css file, you can customize the styles of elements to suit your preferences.

     

    Output:  

    When a user visits the site for the first time and clicks the ‘Open Modal’ button, the following output will be displayed:Cookie Step1

    As you can see above, the modal has appeared. Now, close the modal, and the output below will be displayed:

    Cookie Step2

    After closing the modal, a cookie is stored to remember your preference.

     

    Conclusion:

    This code provides a simple solution for displaying a cookie notice using a modal pop-up. A modal is a small window that appears on top of the webpage content, typically for notifications or alerts. In this case, it informs users about the website’s use of cookies. By setting a dismissed=true cookie when the user closes the modal, the website remembers the user’s preference, preventing the modal from appearing on subsequent visits. This method helps comply with cookie consent regulations while enhancing the user experience by not showing the notice repeatedly once dismissed.

    Source: Read More 

    Hostinger
    Facebook Twitter Reddit Email Copy Link
    Previous Articlemaantje/xhprof-buggregator-laravel
    Next Article Implementing a Color Flipper with JavaScript: A Simple Guide

    Related Posts

    Artificial Intelligence

    Markus Buehler receives 2025 Washington Award

    June 1, 2025
    Artificial Intelligence

    LWiAI Podcast #201 – GPT 4.5, Sonnet 3.7, Grok 3, Phi 4

    June 1, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    CVE-2025-32889 – goTenna Hardcoded Verification Token Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Satya Nadella says AI already writes 30% of Microsoft’s code — but Bill Gates claims software development is too complex to be fully automated

    News & Updates

    CVE-2025-3634 – Moodle Authentication Bypass

    Common Vulnerabilities and Exposures (CVEs)

    Two Actively Exploited Security Flaws in Adobe and Oracle Products Flagged by CISA

    Development

    Highlights

    Electronic Communication Policy

    March 19, 2025

    Bring privacy, confidentiality, and security to your organization with this in-depth Electronic Communication Policy. Customizable…

    Unlocking the hidden power of boiling — for energy, space, and beyond

    January 2, 2025

    Fine-Tuning Llama 3.2 3B Instruct for Python Code: A Comprehensive Guide with Unsloth

    February 4, 2025

    CVE-2025-30322 – Substance3D Painter Out-of-Bounds Write Vulnerability

    May 13, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.