Creating a Cookie Banner which only is visible if a cookie has not been set
Posted: Mon Jul 29, 2019 8:18 pm
This example is about how you can create a cookie banner which appears only if a corresponding cookie has not been set (no accept pressed).
If you press the "accept" button, the SL will go away and a cookie is set.
On each page your SL has been added, the SL will not show up.
When you delete the cookie from your browser and reload the page, the SL will reappear.
Note: Cookie(s) are stored automatically under the domain name of your site. A site can have more cookies but all of them are collected under the domain name. No extra management is needed.
Adopt all namings, formattings and exiration date ect. according your need.
You can use this method not only for a cookie bar but also for other things
- Download https://github.com/js-cookie/js-cookie/ ... .cookie.js which is a very small script
- Make this script available to your site so it gets uploaded to a directory of your choice, eg "scripts"
Use eg. the FilePublisher Object. - Create a Sticky Layer (buttom-right, absolute, center) and add (embed) some text and a button
Best you place the SL in a master frame to make it available to all pages. The name in this example is master_frameCookieLayer
Format all items as you like it, like the SL: CSS3 animation: animate-fade-in-up: 0,1000 - Add two events to that button:
onclick: run javascript: myCookieSet();
onclick: animateCSS3: master_frameCookieLayer: animate-fade-out: 0,500 - Uncheck visibility of the SL in the Object Manager.
This will hide the Sticky Layer not only in WWB but also in the browser when loading. So it is initially not visible ! - In each page you want that the SL could appear, add the following script in the page HTML/inside body tag:
This script adds an event when the particular page is ready and fires the myCookieRead() function in case of.
Code: Select all
<script type="text/javascript"> document.addEventListener('DOMContentLoaded', (event) => { myCookieRead(); }); </script>
- Inside SiteHTML/inside body tag:
Function myCookieRead() checks if the cookie has been set and if not makes the SL visible again.
Code: Select all
<script src="scripts/js.cookie.js"></script> <script type="text/javascript"> function myCookieRead() { var cookie = Cookies.get('cookie_accepted'); if (cookie != "true") { document.getElementById('master_frameCookieLayer').style.visibility='visible'; document.getElementById('master_frameCookieLayer').style.display = "block"; } } function myCookieSet() { Cookies.set('cookie_accepted', 'true', { expires: 365 }); } </script>
Function myCookieSet() sets the cookie for further use. Called if the button has been pressed (accepted)
If you press the "accept" button, the SL will go away and a cookie is set.
On each page your SL has been added, the SL will not show up.
When you delete the cookie from your browser and reload the page, the SL will reappear.
Note: Cookie(s) are stored automatically under the domain name of your site. A site can have more cookies but all of them are collected under the domain name. No extra management is needed.
Adopt all namings, formattings and exiration date ect. according your need.
You can use this method not only for a cookie bar but also for other things
