Instantly share code, notes, and snippets.
Vue.js component for a simple cookie consent banner
// Based on eu_cookie_banner.js by rosell-dk -> https://gist.github.com/rosell-dk/217e1cfdf09bf01bb19f56644be0c6f7 | |
<template> | |
<div id="cookie-law" v-if="!cookieConsent"> | |
<p> | |
🍪 This website uses cookies to enhance your user experience. | |
</p> | |
<p> | |
<a href="#" v-on:click="createCookie"><b>Okay 😌</b></a> | |
</p> | |
</div> | |
</template> | |
<style> | |
#cookie-law { | |
display: flex; | |
align-items: center; | |
position: fixed; | |
bottom: 0; | |
left: 0; | |
right: 0; | |
padding: 5px; | |
z-index: 999; | |
background-color: #dec3ff; | |
justify-content: space-around; | |
} | |
</style> | |
<script> | |
export default { | |
name: 'CookieConsent', | |
data: function(){ | |
return { | |
cookieName: 'MyCookieConsent', | |
cookieDuration: 30, | |
cookieValue: 'accepted', | |
cookieConsent: false | |
} | |
}, | |
computed: { | |
cookieString: function() { | |
return `${this.cookieName}=${this.cookieValue}` | |
} | |
}, | |
methods: { | |
createCookie: function() { | |
var expires; | |
var date = new Date(); | |
date.setTime(date.getTime()+(this.cookieDuration*24*60*60*1000)); | |
expires = "; expires="+date.toGMTString(); | |
document.cookie = this.cookieString+expires+"; path=/"; | |
this.cookieConsent = true; | |
}, | |
deleteCookie: function() { | |
var expires; | |
var date = new Date(); | |
date.setTime(date.getTime()+(-1*24*60*60*1000)); | |
expires = "; expires="+date.toGMTString(); | |
document.cookie = this.cookieString+expires+"; path=/"; | |
this.cookieConsent = false; | |
}, | |
cookieAccepted: function() { | |
return document.cookie.indexOf(this.cookieString) > -1 | |
}, | |
}, | |
mounted() { | |
this.cookieConsent = this.cookieAccepted() | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment