forked from carlwoodhouse/jquery.cookieBar
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.cookieBar.js
87 lines (74 loc) · 2.79 KB
/
jquery.cookieBar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*!
* jQuery Cookiebar Plugin
* https://github.com/carlwoodhouse/jquery.cookieBar
*
* Copyright 2012-17, Carl Woodhouse. the cookie function is inspired by https://github.com/carhartl/jquery-cookie
* Disclaimer: if you still get fined for not complying with the eu cookielaw, it's not our fault.
* Licence: MIT
*/
(function ($) {
$.cookie = function (key, value, options) {
if (arguments.length > 1 && (!/Object/.test(Object.prototype.toString.call(value)) || value === null || value === undefined)) {
options = $.extend({}, options);
if (value === null || value === undefined) {
options.expires = -1;
}
if (typeof options.expires === 'number') {
var days = options.expires, t = options.expires = new Date();
t.setDate(t.getDate() + days);
}
value = String(value);
return (document.cookie = [
encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value),
options.expires ? '; expires=' + options.expires.toUTCString() : '', // max-age is not supported by IE
options.path ? '; path=' + options.path : '',
options.domain ? '; domain=' + options.domain : '',
options.secure ? '; secure' : ''
].join(''));
}
options = value || {};
var decode = options.raw ? function (s) { return s; } : decodeURIComponent;
var pairs = document.cookie.split('; ');
for (var i = 0, pair; pair = pairs[i] && pairs[i].split('='); i++) {
// IE
if (decode(pair[0]) === key) return decode(pair[1] || '');
}
return null;
};
$.fn.cookieBar = function (options) {
var settings = $.extend({
'closeButton': 'none',
'hideOnClose': true,
'secure': false,
'path': '/',
'domain': '',
'expires': 30
}, options);
return this.each(function () {
var cookiebar = $(this);
// just in case they didnt hide it by default.
cookiebar.hide();
// if close button not defined. define it!
if (settings.closeButton == 'none') {
cookiebar.append('<a class="cookiebar-close">Continue</a>');
$.extend(settings, { 'closeButton': '.cookiebar-close' });
}
if ($.cookie('cookiebar') != 'hide') {
cookiebar.show();
}
cookiebar.find(settings.closeButton).click(function () {
if (settings.hideOnClose) {
cookiebar.hide();
}
$.cookie('cookiebar', 'hide', { path: settings.path, secure: settings.secure, domain: settings.domain, expires: settings.expires });
cookiebar.trigger('cookieBar-close');
return false;
});
});
};
// self injection init
$.cookieBar = function (options) {
$('body').prepend('<div class="ui-widget"><div style="display: none;" class="cookie-message ui-widget-header blue"><p>By using this website you allow us to place cookies on your computer. They are harmless and never personally identify you.</p></div></div>');
$('.cookie-message').cookieBar(options);
};
})(jQuery);