Cookie Consent Popup in JavaScript

Cookie Consent Popup in JavaScript
Code Snippet:Cookie Consent Box
Author: Farid Vatani
Published: January 19, 2024
Last Updated: January 22, 2024
Downloads: 768
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript code creates a Cookie Consent Popup that appears when you visit a website. It helps website visitors to either accept or reject the use of cookies for personalized content and traffic analysis.

When a user accepts the cookie policy, a cookie is set with a maximum age of 30 days. If the user has already accepted, the popup is hidden. This code helps you comply with privacy regulations.

How to Create Cookie Consent Popup in JavaScript

1. First of all, load the Google Fonts by adding the following CDN link to the head tag of your website. (Optional)

<link rel='stylesheet' href=';200;300;400;500;600;700;800&amp;display=swap'>

2. Now, create the HTML structure for your cookie consent popup. You can customize the content to suit your website’s needs.

<h2> This page demonstrates Cookie Consent Popup message. </h2>
 <div class="wrapper">
    <img src="" alt="cookie" />
    <div class="content">
      <header>Cookies Consent</header>
      <p>We use our own and third-party cookies to personalize content and to analyze web traffic.</p>
      <div class="buttons">
        <button class="item">Accept</button>
        <a href="#" class="item">Reject</a>

3. Use the following CSS code to style the cookie consent popup. You can modify the CSS to match your website’s design.

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;
body {
  display: flex;
  justify-content: center;
  align-items: center;
  background: #212529;
  min-height: 100vh;

.wrapper {
  position: absolute;
  max-width: 365px;
  background: #fff;
  border: 5px solid #fca311;
  padding: 25px 25px 30px 25px;
  border-radius: 15px;
  box-shadow: 1px 7px 14px -5px rgba(0, 0, 0, 0.15);
  text-align: center;
  top: 100px;
.wrapper.hide {
  opacity: 0;
  pointer-events: none;
  transform: scale(0.8);
  transition: all 0.3s ease;
::selection {
  color: #fff;
  background: #212529;
.wrapper img {
  max-width: 90px;
.content header {
  font-size: 25px;
  font-weight: 600;
.content {
  margin-top: 10px;
.content p {
  text-align: justify;
  color: #858585;
  margin: 5px 0 20px 0;
.content .buttons {
  display: flex;
  align-items: center;
  justify-content: center;
.buttons button {
  padding: 10px 20px;
  border: none;
  outline: none;
  color: #212529;
  font-size: 16px;
  font-weight: 500;
  border-radius: 5px;
  background: #fcba7f;
  cursor: pointer;
  transition: all 0.3s ease;
.buttons button:hover {
  transform: scale(0.97);
.buttons .item {
  margin: 0 10px;
.buttons a {
  text-decoration: none;
  padding: 10px 20px;
  border-radius: 5px;
  color: #212529;
  background: #e9ecef;

4. The JavaScript code handles the functionality of the cookie consent popup. It sets a cookie when the user accepts and checks if the user has already accepted to hide the popup.

const cookieBox = document.querySelector(".wrapper"),
  acceptBtn = cookieBox.querySelector("button");
acceptBtn.onclick = () => {
  document.cookie = "CookieBy=FaridVatani; max-age=" + 60 * 60 * 24 * 30;
  if (document.cookie) {
  } else {
      "Cookie can't be set! Please unblock this site from the cookie setting of your browser."
let checkCookie = document.cookie.indexOf("CookieBy=FaridVatani");
checkCookie != -1
  ? cookieBox.classList.add("hide")
  : cookieBox.classList.remove("hide");

Replace “FaridVatanie” in the JavaScript code with your desired cookie name. You can also adjust the cookie’s maximum age to match your preferences.

That’s all! hopefully, you have successfully created a Cookie Consent message on your website. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link