1/* 2 * Copyright (C) 2012 PrimeBox (info@primebox.co.uk) 3 * 4 * This work is licensed under the Creative Commons 5 * Attribution 3.0 Unported License. To view a copy 6 * of this license, visit 7 * http://creativecommons.org/licenses/by/3.0/. 8 * 9 * Documentation available at: 10 * http://www.primebox.co.uk/projects/cookie-bar/ 11 * 12 * When using this software you use it at your own risk. We hold 13 * no responsibility for any damage caused by using this plugin 14 * or the documentation provided. 15 */ 16(function($){ 17 $.cookieBar = function(options,val){ 18 if(options=='cookies'){ 19 var doReturn = 'cookies'; 20 }else if(options=='set'){ 21 var doReturn = 'set'; 22 }else{ 23 var doReturn = false; 24 } 25 var defaults = { 26 message: 'We use cookies to track usage and preferences.', //Message displayed on bar 27 acceptButton: true, //Set to true to show accept/enable button 28 acceptText: 'I Understand', //Text on accept/enable button 29 acceptFunction: function(cookieValue){if(cookieValue!='enabled' && cookieValue!='accepted') window.location = window.location.href;}, //Function to run after accept 30 declineButton: false, //Set to true to show decline/disable button 31 declineText: 'Disable Cookies', //Text on decline/disable button 32 declineFunction: function(cookieValue){if(cookieValue=='enabled' || cookieValue=='accepted') window.location = window.location.href;}, //Function to run after decline 33 policyButton: false, //Set to true to show Privacy Policy button 34 policyText: 'Privacy Policy', //Text on Privacy Policy button 35 policyURL: '/privacy-policy/', //URL of Privacy Policy 36 autoEnable: true, //Set to true for cookies to be accepted automatically. Banner still shows 37 acceptOnContinue: false, //Set to true to accept cookies when visitor moves to another page 38 acceptOnScroll: false, //Set to true to accept cookies when visitor scrolls X pixels up or down 39 acceptAnyClick: false, //Set to true to accept cookies when visitor clicks anywhere on the page 40 expireDays: 365, //Number of days for cookieBar cookie to be stored for 41 renewOnVisit: false, //Renew the cookie upon revisit to website 42 forceShow: false, //Force cookieBar to show regardless of user cookie preference 43 effect: 'slide', //Options: slide, fade, hide 44 element: 'body', //Element to append/prepend cookieBar to. Remember "." for class or "#" for id. 45 append: false, //Set to true for cookieBar HTML to be placed at base of website. Actual position may change according to CSS 46 fixed: false, //Set to true to add the class "fixed" to the cookie bar. Default CSS should fix the position 47 bottom: false, //Force CSS when fixed, so bar appears at bottom of website 48 zindex: '', //Can be set in CSS, although some may prefer to set here 49 domain: String(window.location.hostname), //Location of privacy policy 50 referrer: String(document.referrer) //Where visitor has come from 51 }; 52 var options = $.extend(defaults,options); 53 54 //Sets expiration date for cookie 55 var expireDate = new Date(); 56 expireDate.setTime(expireDate.getTime()+(options.expireDays*86400000)); 57 expireDate = expireDate.toGMTString(); 58 59 var cookieEntry = 'cb-enabled={value}; expires='+expireDate+'; path=/'; 60 61 //Retrieves current cookie preference 62 var i,cookieValue='',aCookie,aCookies=document.cookie.split('; '); 63 for (i=0;i<aCookies.length;i++){ 64 aCookie = aCookies[i].split('='); 65 if(aCookie[0]=='cb-enabled'){ 66 cookieValue = aCookie[1]; 67 } 68 } 69 //Sets up default cookie preference if not already set 70 if(cookieValue=='' && doReturn!='cookies' && options.autoEnable){ 71 cookieValue = 'enabled'; 72 document.cookie = cookieEntry.replace('{value}','enabled'); 73 }else if((cookieValue=='accepted' || cookieValue=='declined') && doReturn!='cookies' && options.renewOnVisit){ 74 document.cookie = cookieEntry.replace('{value}',cookieValue); 75 } 76 if(options.acceptOnContinue){ 77 if(options.referrer.indexOf(options.domain)>=0 && String(window.location.href).indexOf(options.policyURL)==-1 && doReturn!='cookies' && doReturn!='set' && cookieValue!='accepted' && cookieValue!='declined'){ 78 doReturn = 'set'; 79 val = 'accepted'; 80 } 81 } 82 if(doReturn=='cookies'){ 83 //Returns true if cookies are enabled, false otherwise 84 if(cookieValue=='enabled' || cookieValue=='accepted'){ 85 return true; 86 }else{ 87 return false; 88 } 89 }else if(doReturn=='set' && (val=='accepted' || val=='declined')){ 90 //Sets value of cookie to 'accepted' or 'declined' 91 document.cookie = cookieEntry.replace('{value}',val); 92 if(val=='accepted'){ 93 return true; 94 }else{ 95 return false; 96 } 97 }else{ 98 //Sets up enable/accept button if required 99 var message = options.message.replace('{policy_url}',options.policyURL); 100 101 if(options.acceptButton){ 102 var acceptButton = '<a href="" class="cb-enable">'+options.acceptText+'</a>'; 103 }else{ 104 var acceptButton = ''; 105 } 106 //Sets up disable/decline button if required 107 if(options.declineButton){ 108 var declineButton = '<a href="" class="cb-disable">'+options.declineText+'</a>'; 109 }else{ 110 var declineButton = ''; 111 } 112 //Sets up privacy policy button if required 113 if(options.policyButton){ 114 var policyButton = '<a href="'+options.policyURL+'" class="cb-policy">'+options.policyText+'</a>'; 115 }else{ 116 var policyButton = ''; 117 } 118 //Whether to add "fixed" class to cookie bar 119 if(options.fixed){ 120 if(options.bottom){ 121 var fixed = ' class="fixed bottom"'; 122 }else{ 123 var fixed = ' class="fixed"'; 124 } 125 }else{ 126 var fixed = ''; 127 } 128 if(options.zindex!=''){ 129 var zindex = ' style="z-index:'+options.zindex+';"'; 130 }else{ 131 var zindex = ''; 132 } 133 134 //Displays the cookie bar if arguments met 135 if(options.forceShow || cookieValue=='enabled' || cookieValue==''){ 136 if(options.append){ 137 $(options.element).append('<div id="cookie-bar"'+fixed+zindex+'><p>'+message+'</p><p>'+acceptButton+declineButton+policyButton+'</p></div>'); 138 }else{ 139 $(options.element).prepend('<div id="cookie-bar"'+fixed+zindex+'><p>'+message+'</p><p>'+acceptButton+declineButton+policyButton+'</p></div>'); 140 } 141 } 142 143 var removeBar = function(func){ 144 if(options.acceptOnScroll) $(document).off('scroll'); 145 if(typeof(func)==='function') func(cookieValue); 146 if(options.effect=='slide'){ 147 $('#cookie-bar').slideUp(300,function(){$('#cookie-bar').remove();}); 148 }else if(options.effect=='fade'){ 149 $('#cookie-bar').fadeOut(300,function(){$('#cookie-bar').remove();}); 150 }else{ 151 $('#cookie-bar').hide(0,function(){$('#cookie-bar').remove();}); 152 } 153 $(document).unbind('click',anyClick); 154 }; 155 var cookieAccept = function(){ 156 document.cookie = cookieEntry.replace('{value}','accepted'); 157 removeBar(options.acceptFunction); 158 }; 159 var cookieDecline = function(){ 160 var deleteDate = new Date(); 161 deleteDate.setTime(deleteDate.getTime()-(864000000)); 162 deleteDate = deleteDate.toGMTString(); 163 aCookies=document.cookie.split('; '); 164 for (i=0;i<aCookies.length;i++){ 165 aCookie = aCookies[i].split('='); 166 if(aCookie[0].indexOf('_')>=0){ 167 document.cookie = aCookie[0]+'=0; expires='+deleteDate+'; domain='+options.domain.replace('www','')+'; path=/'; 168 }else{ 169 document.cookie = aCookie[0]+'=0; expires='+deleteDate+'; path=/'; 170 } 171 } 172 document.cookie = cookieEntry.replace('{value}','declined'); 173 removeBar(options.declineFunction); 174 }; 175 var anyClick = function(e){ 176 if(!$(e.target).hasClass('cb-policy')) cookieAccept(); 177 }; 178 179 $('#cookie-bar .cb-enable').click(function(){cookieAccept();return false;}); 180 $('#cookie-bar .cb-disable').click(function(){cookieDecline();return false;}); 181 if(options.acceptOnScroll){ 182 var scrollStart = $(document).scrollTop(),scrollNew,scrollDiff; 183 $(document).on('scroll',function(){ 184 scrollNew = $(document).scrollTop(); 185 if(scrollNew>scrollStart){ 186 scrollDiff = scrollNew - scrollStart; 187 }else{ 188 scrollDiff = scrollStart - scrollNew; 189 } 190 if(scrollDiff>=Math.round(options.acceptOnScroll)) cookieAccept(); 191 }); 192 } 193 if(options.acceptAnyClick){ 194 $(document).bind('click',anyClick); 195 } 196 } 197 }; 198})(jQuery);