-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcookiePopup.js
More file actions
182 lines (136 loc) · 4.84 KB
/
cookiePopup.js
File metadata and controls
182 lines (136 loc) · 4.84 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* @namespace
*/
(
function() {
"use strict";
/**
* @namespace
* @description A simple cookie message popup to be compliant with the EU cookie directive ruling.
*/
CookiePopupManager = {
/**
* @default
* @description Store the configuration and allow some defaults which may be overriden
*/
configuration: {
COOKIE_INFORMATION_URL: '/',
COOKIE_POPUP_POSITION: 'top',
COOKIE_NAME: 'EUCookieAccepted',
COOKIE_VALUE: 'Cookies Accepted'
},
/**
* @function
* @description Create a div with a message and attach it to the body element.
*/
initialise: function (configuration) {
if (configuration !== undefined) {
CookiePopupManager.overrideConfiguration(configuration);
}
if (CookiePopupManager.userHasAcceptedCookies() ) {
return;
}
/**
* Create a popup and store a reference to it
*/
CookiePopupManager.messageContainer = CookiePopupManager.createPopupMessage();
if (CookiePopupManager.messageContainer != null) {
}
},
/**
* @function
* @description Merge the users configuration into our default configuration
*/
overrideConfiguration: function(configuration) {
for(var configurationItemKey in configuration) {
CookiePopupManager.configuration[configurationItemKey] = configuration[configurationItemKey];
}
},
/**
* @function
* @description CreatePopupMessage
*/
createPopupMessage: function () {
var cookieHtmlBuilder = [];
cookieHtmlBuilder.push( '<div id="cookiePopupContainer" style="position: fixed; ');
cookieHtmlBuilder.push( CookiePopupManager.configuration.COOKIE_POPUP_POSITION );
cookieHtmlBuilder.push( ': 0px; background: #fff; border: solid 1px #ccc; padding: 1em; margin: 0; width: auto;">' );
cookieHtmlBuilder.push( 'This site uses cookies. By using this site you agree to allow cookies to be stored in your browser. ');
cookieHtmlBuilder.push( 'For more details visit <a href="' + CookiePopupManager.configuration.COOKIE_INFORMATION_URL + '">' );
cookieHtmlBuilder.push( 'the cookie page</a> for details. ' );
cookieHtmlBuilder.push( '<span style="margin-right: 4em; float:right; ">' );
cookieHtmlBuilder.push( '<a href="#" onclick="return CookiePopupManager.closePopup();">I agree to accept cookies</a></span></div>' );
var cookieHtml = cookieHtmlBuilder.join(' ');
var searchForBody = document.getElementsByTagName('body');
if(searchForBody.length == 1) {
var bodyTag = searchForBody[0];
var popupDiv = document.createElement('div');
popupDiv.innerHTML = cookieHtml;
bodyTag.appendChild(popupDiv);
return popupDiv;
}
/**
* No body element. exit
*/
return null;
},
/**
* @function
* @description Close the cookie popup container.
*/
closePopup: function () {
CookiePopupManager.storeCookie();
var cookiePopupParent = CookiePopupManager.messageContainer.parentNode;
cookiePopupParent.removeChild(CookiePopupManager.messageContainer);
},
/**
* @function
* @description Check for a saved cookie indicating the user has accepted cookies
*/
userHasAcceptedCookies: function() {
var cookieArray = document.cookie.split(";");
var cookieName, cookieValue, cookieSplit;
for (i = 0; i < cookieArray.length;i++) {
if (cookieArray[i].indexOf('=') != -1) {
cookieSplit = cookieArray[i].split('=');
cookieName = cookieSplit[0].replace(/^\s+|\s+$/g, '');
cookieValue = unescape(cookieSplit[1].replace(/^\s+|\s+$/g, ''));
if ((cookieName == CookiePopupManager.configuration.COOKIE_NAME) &&
(cookieValue == CookiePopupManager.configuration.COOKIE_VALUE)) {
return true;
}
}
}
return false;
},
/**
* @function
* @description Store a cookie in the browser
*
* Set the cookie to expire ten years in the future.
* Note - some browsers may potentially have problems with dates
* greater than 2038, although I would be surprised if this hasn't been fixed.
*
* Search for Y2038 issue for more information.
*
* Since we are only setting the cookie once, in ten years time the message will reappear.
* I have decided I would rather that inconvenience than add
* extra code in to reset the expiry on every page refresh.
*
*/
storeCookie: function() {
var expiryDate = new Date();
expiryDate.setDate(expiryDate.getDate() + 365 * 10);
var cookieValue = escape(CookiePopupManager.configuration.COOKIE_VALUE) + '; expires=' + expiryDate.toUTCString();
var previousCookies = document.cookie;
if(previousCookies != '') {
previousCookies = previousCookies + ';';
}
/**
* Add the cookie in
*/
document.cookie = previousCookies + CookiePopupManager.configuration.COOKIE_NAME + "=" + cookieValue;
}
}
}
)();