PageSourceSearch

https://www.horse-ball.org/media/js/ihavecookies-master/jquery.ihavecookies.js

js horse-ball.org collected 2026-09-26 07:36:27 UTC 9,877 bytes, 222 lines download raw bytes

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