I want to set a banner that asks for permission to store cookies on my website(because GDPR). I’ve tried like this: HTML:
JavaScript
x
31
31
1
{% block cookies %}
2
{% if cookies_check %}
3
{% else %}
4
<div class="fixed-bottom p-4" id="cookie-consent-container">
5
<div class="toast bg-dark text-white w-100 mw-100" role="alert">
6
<div class="toast-body p-4 d-flex flex-column">
7
<h4>Cookie Warning</h4>
8
<p>
9
This website stores data such as cookies to enable site functionality including analytics and personalization. By using this website, you automatically accept that we use cookies.
10
</p>
11
<div class="ml-auto">
12
<button type="button" class="btn btn-outline-light mr-3" id="btnDeny">
13
Deny
14
</button>
15
<button type="button" class="btn btn-light" id="btnAccept">
16
Accept
17
</button>
18
</div>
19
</div>
20
</div>
21
</div>
22
<script>
23
var fn = function () {
24
document.cookie = "cookie_consent=true";
25
document.getElementById('cookie-consent-container').hidden = true;
26
};
27
document.getElementById('btnAccept').onclick = fn;
28
</script>
29
{% endif %}
30
{% endblock cookies %}
31
and jinja2
JavaScript
1
13
13
1
@app.context_processor
2
def inject_template_scope():
3
injections = dict()
4
print(injections)
5
6
def cookies_check():
7
value = request.cookies.get('cookie_consent')
8
print(value)
9
return value == 'true'
10
injections.update(cookies_check=cookies_check)
11
12
return injections
13
The print
s are for debugging. The problem is that the list is always empty and the banner is never showing. What should I do to make this work? Does that mean that the site is not generating any cookie to begin with? If so how can I add a cookie when the site is loaded?
Advertisement
Answer
A cookie can be added,read,deleted using document.cookie
JavaScript
1
2
1
document.cookie = "username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC";
2
By default cookie delete after browser is closed.