-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.cookieconsent.0.0.2.js
142 lines (125 loc) · 5.02 KB
/
jquery.cookieconsent.0.0.2.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* cookie consent jQuery plugin by :Dan Wellman
* © 2012 Dan Wellman & Design Haus UK Ltd
* License: GNU GPL v3 ~ http://www.gnu.org/licenses/gpl.html
*
* Requires:
* jQuery (obviously)
* jquery.cookie.js
*
* version 0.0.2
*/
;(function ($) {
"use strict";
$.cookieconsent = function (config) {
var _config = $.extend({}, $.cookieconsent.defaults, config), container, p, question, yes, no, readMore;
if ($.cookie) {
if ($.cookie("cookie_consent")) {
_config.analyticsInject();
_config.analyticsInjected();
} else {
container = $("<div/>", {
id: _config.containerId
});
p = $("<p/>", {
html: _config.message
}).appendTo(container);
question = $("<span/>", {
text: _config.questionText
}).appendTo(p);
yes = $("<a/>", {
id: _config.confirmLinkId,
text: _config.confirmLinkText,
href: "#",
click: function (e) {
e.preventDefault();
_config.consented();
_config.analyticsInject();
_config.analyticsInjected();
$.cookie("cookie_consent", "consented", { expires: _config.cookieExpiry, path: "/" });
_config.cookieSet();
$("#" + _config.containerId).remove();
}
}).appendTo(p);
no = $("<a/>", {
id: _config.denyLinkId,
text: _config.denyLinkText,
href: "#",
click: function (e) {
e.preventDefault();
_config.denied();
$("#" + _config.containerId).remove();
}
}).appendTo(p);
//add classes to elements links if configured
if (_config.questionClass) {
question.addClass(_config.questionClass);
}
if (_config.confirmLinkClass) {
yes.addClass(_config.confirmLinkClass);
}
if (_config.denyLinkClass) {
no.addClass(_config.denyLinkClass);
}
//add a readmore link if configured
if (_config.addReadMoreLink && _config.readMoreHref) {
readMore = $("<a/>", {
href: _config.readMoreHref,
text: _config.readMoreText
}).appendTo(p);
if (_config.readMoreClass) {
readMore.addClass(_config.readMoreClass);
}
if (_config.readMoreRel) {
readMore.attr("rel", _config.readMoreRel);
}
} else if (_config.addReadMoreLink && !_config.readMoreHref) {
console.log("Configuration error: Please remember to set the href when adding a read more link");
}
//append to start or end of specified container
if (_config.insertAtBeginning) {
container.prependTo(_config.widgetContainer);
_config.displayed();
} else {
container.appendTo(_config.widgetContainer);
_config.displayed();
}
}
} else {
console.log("Dependency error: Please ensure the cookie plugin is available");
}
}
$.cookieconsent.defaults = {
containerId: "consentWidget",
message: "This site wishes to use cookies in order to provide a better experience.",
addReadMoreLink: false,
readMoreHref: "",
readMoreClass: "read-more",
readMoreText: "Find out more",
readMoreRel: "",
questionText: "Do you consent?",
questionClass: "",
confirmLinkId: "yes",
confirmLinkText: "Yes",
confirmLinkClass: "",
denyLinkId: "no",
denyLinkText: "No",
denyLinkClass: "",
widgetContainer: "#form1",
insertAtBeginning: true,
cookieExpiry: 3650,
analyticsInject: function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
},
displayed: function() {},
consented: function() {},
denied: function() {},
cookieSet: function() {},
analyticsInjected: function() {}
}
$.fn.cookieconsent = function(config) {
$.cookieconsent(config);
}
}(jQuery));