ihavecookies.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*!
  2. * ihavecookies - jQuery plugin for displaying cookie/privacy message
  3. * v0.3.2
  4. *
  5. * Copyright (c) 2018 Ketan Mistry (https://iamketan.com.au)
  6. * Licensed under the MIT license:
  7. * http://www.opensource.org/licenses/mit-license.php
  8. *
  9. */
  10. (function($) {
  11. /*
  12. |--------------------------------------------------------------------------
  13. | Cookie Message
  14. |--------------------------------------------------------------------------
  15. |
  16. | Displays the cookie message on first visit or 30 days after their
  17. | last visit.
  18. |
  19. | @param event - 'reinit' to reopen the cookie message
  20. |
  21. */
  22. $.fn.ihavecookies = function(options, event) {
  23. var $element = $(this);
  24. // Set defaults
  25. var settings = $.extend({
  26. cookieTypes: [
  27. {
  28. type: 'Site Preferences',
  29. value: 'preferences',
  30. description: 'These are cookies that are related to your site preferences, e.g. remembering your username, site colours, etc.'
  31. },
  32. {
  33. type: 'Analytics',
  34. value: 'analytics',
  35. description: 'Cookies related to site visits, browser types, etc.'
  36. },
  37. {
  38. type: 'Marketing',
  39. value: 'marketing',
  40. description: 'Cookies related to marketing, e.g. newsletters, social media, etc'
  41. }
  42. ],
  43. title: 'Cookies & Privacy',
  44. message: 'Cookies enable you to use shopping carts and to personalize your experience on our sites, tell us which parts of our websites people have visited, help us measure the effectiveness of ads and web searches, and give us insights into user behavior so we can improve our communications and products.',
  45. link: '/privacy-policy',
  46. delay: 2000,
  47. expires: 30,
  48. redirect: '#',
  49. declineBtnLabel: 'Decline',
  50. moreInfoLabel: 'More information',
  51. acceptBtnLabel: 'Accept Cookies',
  52. advancedBtnLabel: 'Customise Cookies',
  53. cookieTypesTitle: 'Select cookies to accept',
  54. fixedCookieTypeLabel:'Necessary',
  55. fixedCookieTypeDesc: 'These are cookies that are essential for the website to work correctly.',
  56. onAccept: function(){},
  57. uncheckBoxes: false
  58. }, options);
  59. var myCookie = getCookie('cookieControl');
  60. var myCookiePrefs = getCookie('cookieControlPrefs');
  61. if (!myCookie || !myCookiePrefs || event == 'reinit') {
  62. // Remove all instances of the cookie message so it's not duplicated
  63. $('#gdpr-cookie-message').remove();
  64. // Set the 'necessary' cookie type checkbox which can not be unchecked
  65. var cookieTypes = '<li><input type="checkbox" name="gdpr[]" value="necessary" checked="checked" disabled="disabled"> <label title="' + settings.fixedCookieTypeDesc + '">' + settings.fixedCookieTypeLabel + '</label></li>';
  66. // Generate list of cookie type checkboxes
  67. preferences = JSON.parse(myCookiePrefs);
  68. $.each(settings.cookieTypes, function(index, field) {
  69. if (field.type !== '' && field.value !== '') {
  70. var cookieTypeDescription = '';
  71. if (field.description !== false) {
  72. cookieTypeDescription = ' title="' + field.description + '"';
  73. }
  74. cookieTypes += '<li><input type="checkbox" id="gdpr-cookietype-' + field.value + '" name="gdpr[]" value="' + field.value + '" data-auto="on"> <label for="gdpr-cookietype-' + field.value + '"' + cookieTypeDescription + '>' + field.type + '</label></li>';
  75. }
  76. });
  77. var declineBtn = settings.redirect? '<button id="gdpr-cookie-advanced" type="button">' + settings.declineBtnLabel + '</button>' : '';
  78. var policyLink = settings.link? '<a href="' + settings.link + '">' + settings.moreInfoLabel + '</a>' : '';
  79. // Display cookie message on page
  80. var cookieMessage = '<div id="gdpr-cookie-message"><h4>' + settings.title + '</h4><p>' + settings.message + ' ' + policyLink + '<div id="gdpr-cookie-types" style="display:none;"><h5>' + settings.cookieTypesTitle + '</h5><ul>' + cookieTypes + '</ul></div><p><button id="gdpr-cookie-accept" type="button">' + settings.acceptBtnLabel + '</button>' + declineBtn + '</p></div>';
  81. setTimeout(function(){
  82. $($element).append(cookieMessage);
  83. $('#gdpr-cookie-message').hide().fadeIn('slow', function(){
  84. // If reinit'ing, open the advanced section of message
  85. // and re-check all previously selected options.
  86. if (event == 'reinit') {
  87. $('#gdpr-cookie-advanced').trigger('click');
  88. $.each(preferences, function(index, field) {
  89. $('input#gdpr-cookietype-' + field).prop('checked', true);
  90. });
  91. }
  92. });
  93. }, settings.delay);
  94. // When accept button is clicked drop cookie
  95. $('body').on('click','#gdpr-cookie-accept', function(){
  96. // Set cookie
  97. dropCookie(true, settings.expires);
  98. // If 'data-auto' is set to ON, tick all checkboxes because
  99. // the user hasn't clicked the customise cookies button
  100. $('input[name="gdpr[]"][data-auto="on"]').prop('checked', true);
  101. // Save users cookie preferences (in a cookie!)
  102. var prefs = [];
  103. $.each($('input[name="gdpr[]"]').serializeArray(), function(i, field){
  104. prefs.push(field.value);
  105. });
  106. setCookie('cookieControlPrefs', encodeURIComponent(JSON.stringify(prefs)), 365);
  107. // Run callback function
  108. settings.onAccept.call(this);
  109. });
  110. // Toggle advanced cookie options
  111. $('body').on('click', '#gdpr-cookie-advanced', function(){
  112. // Uncheck all checkboxes except for the disabled 'necessary'
  113. // one and set 'data-auto' to OFF for all. The user can now
  114. // select the cookies they want to accept.
  115. /*$('input[name="gdpr[]"]:not(:disabled)').attr('data-auto', 'off').prop('checked', false);
  116. $('#gdpr-cookie-types').slideDown('fast', function(){
  117. $('#gdpr-cookie-advanced').prop('disabled', true);
  118. });*/
  119. location.href = settings.redirect;
  120. });
  121. } else {
  122. var cookieVal = true;
  123. if (myCookie == 'false') {
  124. cookieVal = false;
  125. }
  126. dropCookie(cookieVal, settings.expires);
  127. }
  128. // Uncheck any checkboxes on page load
  129. if (settings.uncheckBoxes === true) {
  130. $('input[type="checkbox"].ihavecookies').prop('checked', false);
  131. }
  132. };
  133. // Method to get cookie value
  134. $.fn.ihavecookies.cookie = function() {
  135. var preferences = getCookie('cookieControlPrefs');
  136. return JSON.parse(preferences);
  137. };
  138. // Method to check if user cookie preference exists
  139. $.fn.ihavecookies.preference = function(cookieTypeValue) {
  140. var control = getCookie('cookieControl');
  141. var preferences = getCookie('cookieControlPrefs');
  142. preferences = JSON.parse(preferences);
  143. if (control === false) {
  144. return false;
  145. }
  146. if (preferences === false || preferences.indexOf(cookieTypeValue) === -1) {
  147. return false;
  148. }
  149. return true;
  150. };
  151. /*
  152. |--------------------------------------------------------------------------
  153. | Drop Cookie
  154. |--------------------------------------------------------------------------
  155. |
  156. | Function to drop the cookie with a boolean value of true.
  157. |
  158. */
  159. var dropCookie = function(value, expiryDays) {
  160. setCookie('cookieControl', value, expiryDays);
  161. $('#gdpr-cookie-message').fadeOut('fast', function() {
  162. $(this).remove();
  163. });
  164. };
  165. /*
  166. |--------------------------------------------------------------------------
  167. | Set Cookie
  168. |--------------------------------------------------------------------------
  169. |
  170. | Sets cookie with 'name' and value of 'value' for 'expiry_days'.
  171. |
  172. */
  173. var setCookie = function(name, value, expiry_days) {
  174. var d = new Date();
  175. d.setTime(d.getTime() + (expiry_days*24*60*60*1000));
  176. var expires = "expires=" + d.toUTCString();
  177. document.cookie = name + "=" + value + ";" + expires + ";path=/";
  178. return getCookie(name);
  179. };
  180. /*
  181. |--------------------------------------------------------------------------
  182. | Get Cookie
  183. |--------------------------------------------------------------------------
  184. |
  185. | Gets cookie called 'name'.
  186. |
  187. */
  188. var getCookie = function(name) {
  189. var cookie_name = name + "=";
  190. var decodedCookie = decodeURIComponent(document.cookie);
  191. var ca = decodedCookie.split(';');
  192. for (var i = 0; i < ca.length; i++) {
  193. var c = ca[i];
  194. while (c.charAt(0) == ' ') {
  195. c = c.substring(1);
  196. }
  197. if (c.indexOf(cookie_name) === 0) {
  198. return c.substring(cookie_name.length, c.length);
  199. }
  200. }
  201. return false;
  202. };
  203. }(jQuery));