Skip to content

Instantly share code, notes, and snippets.

@Kn4ppster
Last active July 5, 2018 11:34
Show Gist options
  • Save Kn4ppster/9bba71f0eaeeb784dd86ba95889fe27c to your computer and use it in GitHub Desktop.
Save Kn4ppster/9bba71f0eaeeb784dd86ba95889fe27c to your computer and use it in GitHub Desktop.
Cookie notice to enable and disable script tags.
<script>
// Default options are shown below.
var cookieNotice = new CookieAccept({
class_notice: 'cookie-accept'
, class_allow: 'cookie-accept__allow'
, class_deny: 'cookie-accept__deny'
// Deny cookies always live for session, expiry_days is ignored.
, cookie: {
name: 'accept_cookies'
, expiry_days: 365
, domain: ''
, path: '/'
}
// Attribute name to find on script tags.
, attr_name: 'cookie-accept'
// Callback function to show notice.
, showNotice: function(notice, options) { //
notice.classList.add(options.class_notice+'--show');
}
// Callback function to hide notice.
, hideNotice: function(notice, options) {
notice.classList.remove(options.class_notice+'--show');
}
});
</script>
<!-- Example cookie notice -->
<div id="cookie-accept" class="cookie-accept">
<div class="cookie-accept__container">
<div class="cookie-accept__content">
This website uses cookies to ensure you get the best experience on our website.
</div>
<div class="cookie-accept__controls">
<button id="cookie-accept__deny" class="cookie-accept__deny">Deny</button>
<button id="cookie-accept__allow" class="cookie-accept__allow">Allow Cookies</button>
</div>
</div>
</div>
<script type="text/plain" cookie-accept>
// Script tags must have their type set to 'text/plain' to prevent them executing. The attribute 'cookie-accept' must be added to the tag so that the cookie-accept lib can execute the script if cookies are allowed.
alert('Testing cookies!');
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment