How To Create A Cookie Consent Box using HTML CSS & JavaScript..

 


Hello companions, today in this blog you'll figure out how to make a Cookie Consent Box utilizing HTML CSS and JavaScript. 



There are two kinds of individuals: 



1. The ones who realize what is a treat: They know that these little information documents are essential and pretty much every site is utilizing them to improve the client experience, to gather investigation and so on 



2. The ones who have no clue about what a treat is: They simply get terrified and befuddled when they see an admonition springing up and they don't have a clue what that implies in any case. 



I'm certain that Cookie alerts are not saying anything new to none of these gatherings. 



The truth of the matter is that we need to continue to add treat agree warnings to our destinations until they don't change this franticness. Site proprietors should ensure it agrees with the law.



A treat is a little book record with little bits of information (most extreme size of 4KB) that the web worker stores on the customer PC/program. Treats help to guarantee the client gets the best insight on the site. In this blog, I'll show how you can set treats to the client program as a treat assent box utilizing javascript. 



From the start, on the website page, there is a treat assent confine which you can see the above review picture. In this crate, there is a treat picture, header, short portrayal, catch to acknowledge treats, and a hyperlink for additional insights regarding this treat. This treat box will not stow away regardless of whether you reload the page until you acknowledge the treats for your program. 



When you acknowledge the treat by tapping on the I comprehend button then the treat box will stow away and will not show again until you physically eliminate the treat of this site from your program or it's not terminated. In the event that you block this site from setting treats to your program or this assent box can't set treats to your program then there will seem a mistake ready box. Treats this assent box set will be consequently terminated following one month yet you can increment or decline the time length agreeing you need.

As you most likely are aware, these days each site utilizes a treat to show you important promotions, data and assuming you have likewise a site or venture, you can utilize this treat box subsequent to changing not many lines of codes. I trust you love this treat assent box and the codes to set treats to the client program.

Figure out how to Set Cookies to the User Browser [Source Codes] 

To make this program [Cookie Consent Box]. To start with, you need to make two Files one HTML File and another is CSS File. Subsequent to making these records simply glue the accompanying codes into your document. You can likewise download the source code records of this Cookie Consent Box from the underneath download button.

HTML File (save as  index.html):

<!DOCTYPE  html>

<html lang="en">
<head>
<meta charset="UTF-8">
 <title>Cookie Constent Box |</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="wrapper">
<img src="#" alt="">
<div class="content">
<header>Cookies Consent</header>
<p>This website use cookies to ensure you get the best experience on our website.</p>
<div class="buttons">
<button class="item">I understand</button>
<a href="#" class="item">Learn more</a>
</div>
</div>
</div>

<script>
const cookieBox = document.querySelector(".wrapper"),
acceptBtn = cookieBox.querySelector("button");

acceptBtn.onclick = ()=>{
//setting cookie for 1 month, after one month it'll be expired automatically
document.cookie = "CookieBy=Codewithharsh; max-age="+60*60*24*30;
if(document.cookie){ //if cookie is set
cookieBox.classList.add("hide"); //hide cookie box
}else{ //if cookie not set then alert an error
alert("Cookie can't be set! Please unblock this site from the cookie setting of your browser.");
}
}
let checkCookie = document.cookie.indexOf("CookieBy=Codewithharsh"); //checking our cookie
//if cookie is set then hide the cookie box else show it
checkCookie != -1 ? cookieBox.classList.add("hide") : cookieBox.classList.remove("hide");
</script>

</body>
</html>

CSS File (save as style.css)
*{
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Poppins', sans-serif;
}
body{
background: #FCBA7F;
}
.wrapper{
position: absolute;
bottom: 30px;
left: 30px;
max-width: 365px;
background: #fff;
padding: 25px 25px 30px 25px;
border-radius: 15px;
box-shadow: 1px 7px 14px -5px rgba(0,0,0,0.15);
text-align: center;
}
.wrapper.hide{
opacity: 0;
pointer-events: none;
transform: scale(0.8);
transition: all 0.3s ease;
}
::selection{
color: #fff;
background: #FCBA7F;
}
.wrapper img{
max-width: 90px;
}
.content header{
font-size: 25px;
font-weight: 600;
}
.content{
margin-top: 10px;
}
.content p{
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: #fff;
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{
color: #FCBA7F;
}


THANKS FOR VISITING

Comments