1/*! 2 * ihavecookies - jQuery plugin for displaying cookie/privacy message 3 * 4 * Copyright (c) 2018 Ketan Mistry (https://iamketan.com.au) 5 * 6 * Licensed under the MIT license: 7 * http://www.opensource.org/licenses/mit-license.php 8 * 9 * Version: 0.3 10 * 11 */ 12(function($) { 13 14 /* 15 |-------------------------------------------------------------------------- 16 | Cookie Message 17 |-------------------------------------------------------------------------- 18 | 19 | Displays the cookie message on first visit or 30 days after their 20 | last visit. 21 | 22 | Options: 23 | - title : Title for the popup 24 | - message : Message to display within the popup 25 | - link : Link to privacy page 26 | - delay : Time before the popup is displayed after page load 27 | - expires : Days for the cookie to expire 28 | - onAccept : Optional callback function when 'Accept' button is clicked 29 | - uncheckBoxes : Unchecks all checkboxes on page load that have class 30 | .ihavecookies applied to them. Set to true to turn this 31 | option on 32 | - moreInfoLabel : Label for the link to privacy policy 33 | - acceptBtnLabel : Label for the accept cookies button 34 | - cookieTypes : Array of cookie types to list with checkboxes 35 | 36 */ 37 $.fn.ihavecookies = function(options) { 38 39 var $element = $(this); 40 41 // Set defaults 42 var settings = $.extend({ 43 cookieTypes: [ 44 { 45 type: 'Site Preferences', 46 value: 'preferences', 47 description: 'These are cookies that are related to your site preferences, e.g. remembering your username, site colours, etc.' 48 }, 49 { 50 type: 'Analytics', 51 value: 'analytics', 52 description: 'Cookies related to site visits, browser types, etc.' 53 }, 54 { 55 type: 'Marketing', 56 value: 'marketing', 57 description: 'Cookies related to marketing, e.g. newsletters, social media, etc' 58 } 59 ], 60 title: 'Cookies & Privacy', 61 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.', 62 link: '/privacy/', 63 delay: 2000, 64 expires: 15, 65 moreInfoLabel: 'More information', 66 acceptBtnLabel: 'Accept Cookies', 67 advancedBtnLabel: 'Customise Cookies', 68 onAccept: function(){}, 69 uncheckBoxes: false 70 }, options); 71 72 var myCookie = getCookie('cookieControl'); 73 var myCookiePrefs = getCookie('cookieControlPrefs'); 74 if (!myCookie || !myCookiePrefs) { 75 76 // Set the 'necessary' cookie type checkbox which can not be unchecked 77 var cookieTypes = '<li><input type="checkbox" name="gdpr[]" value="necessary" checked="checked" disabled="disabled"> <label title="These are cookies that are essential for the website to work correctly.">Necessary</label></li>'; 78 79 // Generate list of cookie type checkboxes 80 $.each(settings.cookieTypes, function(index, field) { 81 if (field.type !== '' && field.value !== '') { 82 var cookieTypeDescription = ''; 83 if (field.description !== false) { 84 cookieTypeDescription = ' title="' + field.description + '"'; 85 } 86 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>'; 87 } 88 }); 89 90 // Display cookie message on page 91 var cookieMessage = '<div id="gdpr-cookie-message"><h4>' + settings.title + '</h4><p>' + settings.message + ' <a href="' + settings.link + '">' + settings.moreInfoLabel + '</a><div id="gdpr-cookie-types" style="display:none;"><h5>Select cookies to accept</h5><ul>' + cookieTypes + '</ul></div><p><button id="gdpr-cookie-accept" type="button">' + settings.acceptBtnLabel + '</button><button id="gdpr-cookie-advanced" type="button">' + settings.advancedBtnLabel + '</button></p></div>'; 92 setTimeout(function(){ 93 $($element).append(cookieMessage); 94 $('#gdpr-cookie-message').hide().fadeIn('slow'); 95 }, settings.delay); 96 97 // When accept button is clicked drop cookie 98 $('body').on('click','#gdpr-cookie-accept', function(){ 99 // Set cookie 100 dropCookie(true, settings.expires); 101 102 // If 'data-auto' is set to ON, tick all checkboxes because 103 // the user hasn't clicked the customise cookies button 104 $('input[name="gdpr[]"][data-auto="on"]').prop('checked', true); 105 106 // Save users cookie preferences (in a cookie!) 107 var prefs = []; 108 $.each($('input[name="gdpr[]"]').serializeArray(), function(i, field){ 109 prefs.push(field.value); 110 });
111 setCookie('cookieControlPrefs', JSON.stringify(prefs), 15); 112 113 // Run callback function 114 settings.onAccept.call(this); 115 }); 116 117 // Toggle advanced cookie options 118 $('body').on('click', '#gdpr-cookie-advanced', function(){ 119 // Uncheck all checkboxes except for the disabled 'necessary' 120 // one and set 'data-auto' to OFF for all. The user can now 121 // select the cookies they want to accept. 122 $('input[name="gdpr[]"]:not(:disabled)').attr('data-auto', 'off').prop('checked', false); 123 $('#gdpr-cookie-types').slideDown('fast', function(){ 124 $('#gdpr-cookie-advanced').prop('disabled', true); 125 }); 126 }); 127 128 } else { 129 var cookieVal = true; 130 if (myCookie == 'false') { 131 cookieVal = false; 132 } 133 dropCookie(cookieVal, settings.expires); 134 } 135 136 // Uncheck any checkboxes on page load 137 if (settings.uncheckBoxes === true) { 138 $('input[type="checkbox"].ihavecookies').prop('checked', false); 139 } 140 141 }; 142 143 // Method to get cookie value 144 $.fn.ihavecookies.cookie = function() { 145 var preferences = getCookie('cookieControlPrefs'); 146 return JSON.parse(preferences); 147 }; 148 149 // Method to check if user cookie preference exists 150 $.fn.ihavecookies.preference = function(cookieTypeValue) { 151 var control = getCookie('cookieControl'); 152 var preferences = getCookie('cookieControlPrefs'); 153 preferences = JSON.parse(preferences); 154 if (control === false) { 155 return false; 156 } 157 if (preferences === false || preferences.indexOf(cookieTypeValue) === -1) { 158 return false; 159 } 160 return true; 161 }; 162 163 /* 164 |-------------------------------------------------------------------------- 165 | Drop Cookie 166 |-------------------------------------------------------------------------- 167 | 168 | Function to drop the cookie with a boolean value of true. 169 | 170 */ 171 var dropCookie = function(value, expiryDays) { 172 setCookie('cookieControl', value, expiryDays); 173 $('#gdpr-cookie-message').fadeOut('fast', function() { 174 $(this).remove(); 175 }); 176 }; 177 178 /* 179 |-------------------------------------------------------------------------- 180 | Set Cookie 181 |-------------------------------------------------------------------------- 182 | 183 | Sets cookie with 'name' and value of 'value' for 'expiry_days'. 184 | 185 */ 186 var setCookie = function(name, value, expiry_days) { 187 var d = new Date(); 188 d.setTime(d.getTime() + (expiry_days*24*60*60*1000)); 189 var expires = "expires=" + d.toUTCString(); 190 document.cookie = name + "=" + value + ";" + expires + ";path=/"; 191 return getCookie(name); 192 }; 193 194 /* 195 |-------------------------------------------------------------------------- 196 | Get Cookie 197 |-------------------------------------------------------------------------- 198 | 199 | Gets cookie called 'name'. 200 | 201 */ 202 var getCookie = function(name) { 203 var cookie_name = name + "="; 204 var decodedCookie = decodeURIComponent(document.cookie); 205 var ca = decodedCookie.split(';'); 206 for (var i = 0; i < ca.length; i++) { 207 var c = ca[i]; 208 while (c.charAt(0) == ' ') { 209 c = c.substring(1); 210 } 211 if (c.indexOf(cookie_name) === 0) { 212 return c.substring(cookie_name.length, c.length); 213 } 214 } 215 return false; 216 }; 217 218}(jQuery));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.