Asked by:
Persistent Cookie for whole domain (Eu privacy)

Question
-
User-841452440 posted
I'm trying to :
- display a Cookie Policy alert on first entrance to a website (this part is working), and then
- stop displaying the notice on other subsequent pages visited on the site.
Here's my code based on https://www.mikesdotnetting.com/article/281/implementing-googles-eu-end-user-consent-policy
Would appreciate some thoughts. thanks
<script type="text/javascript"> $(function () { if (document.cookie.indexOf("cookies") < 0) { $('#cookie-consent').slideDown('slow'); } $('#consent').on('click', function () { document.cookie = "cookies=yes;path=/;domain=mysite.com; Secure; max-age=" + (5*365*24*60*60) ; $('#cookie-consent').slideUp('slow'); }); $('#learn-more').on('click', function () { location.href = '/terms-and-conditions'; }) }); </script>
Wednesday, January 24, 2018 12:36 PM
All replies
-
User283571144 posted
Hi imarkphillips,
stop displaying the notice on other subsequent pages visited on the site.In my opinion, if you don't want show the alert on other subsequent pages, you could just not add the javascript to that page, the notice is fired by the jquery.
But, I don't suggest you disable the notice alter on other subsequent pages.The javascript logic has checked the cookie index, if there are no cookie, it will show.
Besides, you could also use
window.location.href
to get the current url.Then you could write logic to check the url is equal with the default website url, then show the notice.
More details, you could refer to below codes:
<script type="text/javascript"> $(function () { if (window.location.href == "aaaa") { if (document.cookie.indexOf("cookies") < 0) { $('#cookie-consent').slideDown('slow'); } $('#consent').on('click', function () { document.cookie = "cookies=yes;path=/;domain=mysite.com; Secure; max-age=" + (5 * 365 * 24 * 60 * 60); $('#cookie-consent').slideUp('slow'); }); $('#learn-more').on('click', function () { location.href = '/terms-and-conditions'; }) } }); </script>
Best Regards,
Brando
Thursday, January 25, 2018 5:20 AM -
User-841452440 posted
Thanks for those thoughts Brando.
We will need the javascript on every page however as we don't know which actual page the user will enter the site via.
We could restrict the notice to the homepage alone and hope the user goes there at some stage but it's not my preferred option.
regards
Mark
Thursday, January 25, 2018 6:31 AM -
User283571144 posted
Hi imarkphillips,
According to your description, I suggest you could consider using local storage to store the user accessed time(you need a way to get the user access time).
If the user has accessed your site, then you could store the true in the local storage.
Then you could write check logic in the javascript.
About how to use local storage, you could refer to below link:
https://www.smashingmagazine.com/2010/10/local-storage-and-how-to-use-it/
Best Regards,
Brando
Friday, January 26, 2018 5:58 AM