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

      Sunshine And March Vibes (2025 Wallpapers Edition)

      June 2, 2025

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

      June 2, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      June 2, 2025

      How To Prevent WordPress SQL Injection Attacks

      June 2, 2025

      How Red Hat just quietly, radically transformed enterprise server Linux

      June 2, 2025

      OpenAI wants ChatGPT to be your ‘super assistant’ – what that means

      June 2, 2025

      The best Linux VPNs of 2025: Expert tested and reviewed

      June 2, 2025

      One of my favorite gaming PCs is 60% off right now

      June 2, 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

      `document.currentScript` is more useful than I thought.

      June 2, 2025
      Recent

      `document.currentScript` is more useful than I thought.

      June 2, 2025

      Adobe Sensei and GenAI in Practice for Enterprise CMS

      June 2, 2025

      Over The Air Updates for React Native Apps

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

      You can now open ChatGPT on Windows 11 with Win+C (if you change the Settings)

      June 2, 2025
      Recent

      You can now open ChatGPT on Windows 11 with Win+C (if you change the Settings)

      June 2, 2025

      Microsoft says Copilot can use location to change Outlook’s UI on Android

      June 2, 2025

      TempoMail — Command Line Temporary Email in Linux

      June 2, 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.

    Hostinger

     

    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 

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

    Related Posts

    Development

    A Beginner’s Guide to Graphs — From Google Maps to Chessboards

    June 2, 2025
    Development

    How to Code Linked Lists with TypeScript: A Handbook for Developers

    June 2, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    CVE-2025-3807 – Zhenfeng13 My-BBS Unrestricted File Upload Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Google AI Releases Gemini 2.0 Flash Thinking model (gemini-2.0-flash-thinking-exp-01-21): Scoring 73.3% on AIME (Math) and 74.2% on GPQA Diamond (Science) Benchmarks

    Machine Learning

    CVE-2025-47931 – LibreNMS Stored Cross-Site Scripting (XSS) Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Great-OS – Linux distribution

    Linux

    Highlights

    Development

    Researchers at Stanford Explore the Potential of Mid-Sized Language Models for Clinical QA (Question-Answering) Tasks

    May 3, 2024

    Recently, there has been remarkable performance on clinical question-answer (QA) tasks by large language models…

    15 Essential PowerShell commands every Windows 11 users should know

    January 15, 2025

    Got Big Ideas? Launch Them with the Help of This $25 Bundle.

    November 1, 2024

    This AI Paper by Toyota Research Institute Introduces SUPRA: Enhancing Transformer Efficiency with Recurrent Neural Networks

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

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