Files
notifications-admin/app/assets/javascripts/govuk/cookie-functions.js
Tom Byers ab4ec7197a Put cookies message back in
The cookie_message block was part of GOV.UK
template but is not included in the GOV.UK
Frontend template.

This adds it back in along with JS to set the
cookies from GOV.UK template and styles, taken
from the Design System's website (which I assume
has the right colour contrast).
2019-11-27 14:15:32 +00:00

63 lines
1.6 KiB
JavaScript

(function () {
"use strict";
var root = this;
if(typeof root.GOVUK === 'undefined') { root.GOVUK = {}; }
/*
Cookie methods
==============
Usage:
Setting a cookie:
GOVUK.cookie('hobnob', 'tasty', { days: 30 });
Reading a cookie:
GOVUK.cookie('hobnob');
Deleting a cookie:
GOVUK.cookie('hobnob', null);
*/
GOVUK.cookie = function (name, value, options) {
if(typeof value !== 'undefined'){
if(value === false || value === null) {
return GOVUK.setCookie(name, '', { days: -1 });
} else {
return GOVUK.setCookie(name, value, options);
}
} else {
return GOVUK.getCookie(name);
}
};
GOVUK.setCookie = function (name, value, options) {
if(typeof options === 'undefined') {
options = {};
}
var cookieString = name + "=" + value + "; path=/";
if (options.days) {
var date = new Date();
date.setTime(date.getTime() + (options.days * 24 * 60 * 60 * 1000));
cookieString = cookieString + "; expires=" + date.toGMTString();
}
if (document.location.protocol == 'https:'){
cookieString = cookieString + "; Secure";
}
document.cookie = cookieString;
};
GOVUK.getCookie = function (name) {
var nameEQ = name + "=";
var cookies = document.cookie.split(';');
for(var i = 0, len = cookies.length; i < len; i++) {
var cookie = cookies[i];
while (cookie.charAt(0) == ' ') {
cookie = cookie.substring(1, cookie.length);
}
if (cookie.indexOf(nameEQ) === 0) {
return decodeURIComponent(cookie.substring(nameEQ.length));
}
}
return null;
};
}).call(this);