Skip to content

Instantly share code, notes, and snippets.

@johnfmorton
Last active May 25, 2018 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnfmorton/a9090656b4d0f54f4ce495521dcd9bcb to your computer and use it in GitHub Desktop.
Save johnfmorton/a9090656b4d0f54f4ce495521dcd9bcb to your computer and use it in GitHub Desktop.
This gist is to archive the way we code a "privacy bar", a bit of legal text that must appear on each viewing of a page on a site. The act of closing the privacy bar once sets a cookie that will make that privacy notice not appear again until the cookie is deleted or expires.
/*
This gist is to archive the way we code a "privacy bar", a bit of legal text that must appear
on each viewing of a page on a site. The act of closing the privacy bar once sets a cookie
that will make that privacy notice not appear again until the cookie is deleted or expires.
<div class="privacy-bar" id="privacy-bar" style="display: block;">
<div class="privacy-bar-content">
<p>We use cookies to make interactions with our websites and services easy and meaningful,
to better understand how they are used and to tailor advertising. You can read more and make
your cookie choices&nbsp;<a href="#">here</a>. By continuing to use this site you are giving
us your consent to do this.</p>
</div>
<div id="privacy-bar-close">
<span alt="Close button" class="privacy-bar-close-button"></span>
</div>
</div>
*/
// assume jQuery is available
var jmx2Base = jmx2Base || {};
jmx2Base.privacyBar = jmx2Base.privacyBar || {};
(function($){
"use strict";
jmx2Base.privacyBar.getCookie = function(theKey) {
if (!theKey) { return null; }
return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" +
encodeURIComponent(theKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
};
jmx2Base.privacyBar.loadPrivacyBar = function() {
var privacybar = $(".privacy-bar");
jmx2Base.privacyBar.validatePrivaceBar(privacybar, jmx2Base.privacyBar.getCookie('privacy_bar'));
};
jmx2Base.privacyBar.validatePrivaceBar = function(privacybar, cookiePrivacyBar) {
if (privacybar && cookiePrivacyBar !== "hidden") {
privacybar.show();
$("#privacy-bar-close").click(function () {
privacybar.hide();
jmx2Base.privacyBar.setCookie();
});
}
};
jmx2Base.privacyBar.setCookie = function() {
// 30*24*60*60 = 2592000 seconds = 30 days
jmx2Base.utils.setCookie('hidden', 'privacy_bar', 30*24*60*60, location.hostname);
};
$(document).ready(function () {
jmx2Base.privacyBar.loadPrivacyBar();
});
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment