Live Chat

Our Blogs

Our every actions and thoughts.


Warning: Undefined variable $title in /var/www/vhosts/hostspacer.com/httpdocs/pages/blogs/index.php on line 13

media

CookieAlert JQuery Plugin for Cookie Policy

/* * cookieAlert Plugin v1.0. * Copyright(c) 2009 Host Spacer Pvt. Ltd. * Contact us at info@hostspacer.com * Author: Shivasis Biswal (Krushna) * Usage: * $(document).ready(function(){ * $('body').cookieAlert(); * }); * Themes: dark, green, light * Example: * $(document).ready(function(){ * $('body').cookieAlert({ theme: 'green'}); * }); * Width: custom width as you wish * Example: * $(document).ready(function(){ * $('body').cookieAlert({ theme: 'green', width: '500'}); * }); * * Position / Place: top-left, top-right, bottom-left, bottom-right * Example: * $(document).ready(function(){ * $('body').cookieAlert({ theme: 'green', width: '500', place: 'top-left'}); * }); * */ ;(function ($) { "use strict"; $.fn.cookieAlert = function(opts) { var hsTitle = 'Accept Cookie Policy'; var hsContent = 'We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you accept and understand our Privacy Policy and our Terms of Service.'; var defaults = { theme: 'dark', width: '400', place: 'b0ttom-left', title: hsTitle, content: hsContent, overlay: true, action: '' }; var settings = $.extend({}, defaults, opts); var hsCook = { init: function(){ if (hsCook.getCookie("cookiesAccepted")) { hsCook.removeAlert(); } else { hsCook.renderAlert(); hsCook.showAlert(); } var el_cookie_alert = document.querySelector(".cookie_alert"); if(el_cookie_alert){ el_cookie_alert.offsetHeight; } $(document).on("click", '.cookie_accept', function (e) { if($(e.target) && $(e.target).parent().hasClass('cookie_accept')) { hsCook.setCookie("cookiesAccepted", true, 180); hsCook.removeAlert(); } }); $(document).on("click", '.cookie_close', function (e) { if($(e.target) && $(e.target).parent().hasClass('cookie_close')) { //hsCook.setCookie("cookiesAccepted", true, 180); hsCook.removeAlert(); } }); }, renderAlert: function() { var alertTitle = settings.title; var alertContent = settings.content; var alertHtml = ''+ ''+ ''+ ''; $('').appendTo('body'); if(settings.overlay == true){ $('').insertAfter('.cookie_alert'); } $('.cookie_alert').append(alertHtml); var cookiealert = $('.cookie_alert'); if(settings.width){ cookiealert.css('width', settings.width + 'px'); } var place = settings.place; switch(place){ case "top-left": cookiealert.css({ top: 2 + '%', left: 1 + '%' }); break; case "top-right": cookiealert.css({ top: 2 + '%', right: 1 + '%' }); break; case "bottom-left": cookiealert.css({ bottom: 2 + '%', left: 1 + '%' }); break; case "bottom-right": cookiealert.css({ bottom: 2 + '%', right: 1 + '%' }); break; default: cookiealert.css({ bottom: 2 + '%', left: 1 + '%' }); } }, showAlert: function(){ if(!$('.cookie_overlay').is(':visible')){ $('.cookie_overlay').show(); } if(!$('.cookie_alert').is(':visible')){ $('.cookie_alert').show(); } }, removeAlert: function(){ if($('.cookie_alert').is(':visible')){ $('.cookie_overlay').remove(); $('.cookie_alert').remove(); } }, // cookie functions setCookie: function (cname, cvalue, exdays) { var d = new Date(); d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000)); var expires = "expires=" + d.toUTCString(); document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/"; }, getCookie: function (cname) { var name = cname + "="; var decodedCookie = decodeURIComponent(document.cookie); var ca = decodedCookie.split(';'); for (var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) === ' ') { c = c.substring(1); } if (c.indexOf(name) === 0) { return c.substring(name.length, c.length); } } return ""; } }; return this.each(function(){ hsCook.init(); }); }; })(jQuery);
.cookie_overlay { position:fixed; top:0; left:0; right:0; bottom:0; width:100%; height:100%; margin:0; padding:0; background: rgba(0,0,0,.5); opacity:0.5; filter:alpha(opacity=50); -moz-opacity: 0.5; z-index:99999; display:none; } .cookie_alert { display: none; position: fixed; padding: 20px; z-index: 999999; box-shadow: 3px 3px 5px #000; } .cookie_close { position: absolute; top: 5px; right: 10px; font-size: 24px; } .cookie_alert_head { font-weight: bold; font-size: 22px; padding: 10px 0; text-transform: uppercase; } .cookie_alert_body { line-height: 150%; font-size: 14px; padding: 10px 0; } .cookie_accept { padding: 10px 0; } /* DARK THEME */ .cookie_alert.dark { background: #555; color: #fff; } .dark .cookie_accept { border-top: 1px solid #999; } .dark .cookie_alert_head { border-bottom: 1px solid #999; } .dark .cookie_close a { color: #999; } .dark .cookie_close a:hover { color: #fff; text-decoration: none; } /* GREEN THEME */ .cookie_alert.green { background: #339966; color: #fff; } .green .cookie_accept { border-top: 1px solid #46C184; } .green .cookie_alert_head { border-bottom: 1px solid #46C184; } .green a { color: #C2EBD7; } .green .cookie_close a { color: #C2EBD7; } .green .cookie_close a:hover { color: #fff; text-decoration: none; } .cookie_alert.light { background: #fcfcfc; color: #777; } .light .cookie_accept { border-top: 1px solid #d4d4d4; } .light .cookie_alert_head { border-bottom: 1px solid #d4d4d4; } .light .cookie_close a { color: #aaa; } .light .cookie_close a:hover { color: #444; text-decoration: none; }

How to use

Basic Usage:

$(document).ready(function(){
    $('body').cookieAlert();
});

Theme: dark, green, light

Example:
$(document).ready(function(){
  $('body').cookieAlert({ theme: 'green'});
});

Width: custom width as you wish

Example:
$(document).ready(function(){
   $('body').cookieAlert({ theme: 'green', width: '500'});
});

Place: top-left, top-right, bottom-left, bottom-right

Example:
 $(document).ready(function(){
 	$('body').cookieAlert({ theme: 'green', width: '500', place: 'top-left'});
 });

Overlay: true, false

Example:
 $(document).ready(function(){
 	$('body').cookieAlert({ theme: 'green', width: '500', place: 'top-left', overlay: true});
 });

Title: Custom title as you wish

Example:
 $(document).ready(function(){
 	$('body').cookieAlert({ 
                         theme: 'green', 
                         width: '500', 
                         place: 'top-left', 
                         overlay: true, 
                         title: 'Cookie Policy'
        });
 });

Content: Custom content as you like

Example:
 $(document).ready(function(){
         var mytitle = 'Cookie Policy';

         var mycontent = 'We use cookies to ensure that we give you the best experience on our website.';

 	$('body').cookieAlert({ 
                         theme:    'green', 
                         width:    '500', 
                         place:    'top-left', 
                         overlay:   true, 
                         title:     mytitle, 
                        content:    mycontent
         });
 });