Cookie Popup

by Venkatesh Dematti

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<div id="cookieAcceptBar" class="cookieAcceptBar">
  Before browse our site, please accept our <a href="#">cookies policy</a><br> <button id="cookieAcceptBarConfirm" class="btn btn-success">Accept and close this alert</button>
</div>

CSS

* {
  font-family: sans-serif;
}

.cookieAcceptBar {
  display: none;
  position: fixed;
  top: 50%;
  left: 0;
  right: 0;
  text-align: center;
  background-color: #333;
  color: #fff;
  padding: 20px 0;
  z-index: 99999;
  -webkit-transform: translateY(-50%);
  transform: translateY(-50%);
}

.cookieAcceptBar a {
  color: #fff;
  text-decoration: none;
  font-weight: bold;
}

button {
  cursor: pointer;
  border: none;
  background-color: #2387c0;
  color: #fff;
  text-transform: uppercase;
  margin-top: 10px;
  height: 40px;
  line-height: 40px;
  padding: 0 20px;
}

JavaScript

jQuery(document).ready(function(){
	cookiesPolicyBar();
});

function cookiesPolicyBar(){
    // Check cookie 
    if (jQuery.cookie('yourCookieName') != "active"){
    	jQuery('#cookieAcceptBar').show();
    }  
    //Assign cookie on click
    jQuery('#cookieAcceptBarConfirm').click(function(){
    	jQuery.cookie('yourCookieName', 'active', { expires: 1 }); // cookie will expire in one day
        jQuery('#cookieAcceptBar').fadeOut();
    });
}