Simple Cookie Consent - How to assess if the user has already clicked and consented
I have been told to add a cookie to a site. The site doesn't retain any user information other than the amount of visits the site gets.
I therefore do not have any user data to assign the cookie to so cannot tell if the user has already accepted.
Keeping the answer as quick to implement as possible and using simple code is it possible to only show the cookie to users that haven't already acknowledged it?
Heres where I am at. 'a basic cookie policy that will show on every page load'. Please help me make it only show once per user.
$(document).ready(function(){
if(getCookie("HideCookie") != "true") {
$("#cookie-consent").css('display','block');
}
});
function SetCookieAndHideDiv(){
setCookie('HideCookie','true',1);
$("#cookie-consent").css('display','none');
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
/* COOKIE */
#cookie-consent {
display: none;
width:100%;
height: 100%;
position:fixed;
bottom:0px;
background-color: rgba(0,0,0,0.8);
z-index: 100000;
}
.cookie-consent-inner {
width:100%;
padding: 20px;
position:fixed;
bottom:0px;
background-color: rgba(0,74,119,1);
color: rgba(255,209,0,1);
}
.cookie {
width: 100%;
max-width: 1248px;
margin: 0 auto;
display: -webkit-flex;
-webkit-flex-wrap: wrap;
display: flex;
flex-wrap: wrap;
align-items: center;
}
.cookie-msg {
width: calc(100% - 120px);
margin-right: 20px;
float: left;
}
.cookie-accept {
width: 100px;
float: right;
}
.cookie-button {
font-size: 16px;
line-height: 40px;
padding: 0px;
color: #FFD100;
width: 100px;
background-color: #337ab7;
cursor: pointer;
border: 1px solid white!important;
-o-transition: all 0.4s linear;
-moz-transition: all 0.4s linear;
-ms-transition: all 0.4s linear;
-webkit-transition: all 0.4s linear;
transition: all 0.4s linear;
text-align: center;
}
.cookie-button:hover {
background-color: #0072AE;
}
/* END COOKIE */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section id="cookie-consent" class="cookie-consent">
<div class="cookie-consent-inner">
<div class="cookie">
<div class="cookie-msg">We use cookies to track visits to our website, we store no personal details. By staying on the site you consent to our cookie policy.</div>
<div class="cookie-accept">
<button id="cookie" class="cookie-button" onclick='SetCookieAndHideDiv();'>OK</button>
</div>
</div>
</div>
</section>
I know this is very basic, but it has basic needs.
php jquery html cookies
|
show 7 more comments
I have been told to add a cookie to a site. The site doesn't retain any user information other than the amount of visits the site gets.
I therefore do not have any user data to assign the cookie to so cannot tell if the user has already accepted.
Keeping the answer as quick to implement as possible and using simple code is it possible to only show the cookie to users that haven't already acknowledged it?
Heres where I am at. 'a basic cookie policy that will show on every page load'. Please help me make it only show once per user.
$(document).ready(function(){
if(getCookie("HideCookie") != "true") {
$("#cookie-consent").css('display','block');
}
});
function SetCookieAndHideDiv(){
setCookie('HideCookie','true',1);
$("#cookie-consent").css('display','none');
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
/* COOKIE */
#cookie-consent {
display: none;
width:100%;
height: 100%;
position:fixed;
bottom:0px;
background-color: rgba(0,0,0,0.8);
z-index: 100000;
}
.cookie-consent-inner {
width:100%;
padding: 20px;
position:fixed;
bottom:0px;
background-color: rgba(0,74,119,1);
color: rgba(255,209,0,1);
}
.cookie {
width: 100%;
max-width: 1248px;
margin: 0 auto;
display: -webkit-flex;
-webkit-flex-wrap: wrap;
display: flex;
flex-wrap: wrap;
align-items: center;
}
.cookie-msg {
width: calc(100% - 120px);
margin-right: 20px;
float: left;
}
.cookie-accept {
width: 100px;
float: right;
}
.cookie-button {
font-size: 16px;
line-height: 40px;
padding: 0px;
color: #FFD100;
width: 100px;
background-color: #337ab7;
cursor: pointer;
border: 1px solid white!important;
-o-transition: all 0.4s linear;
-moz-transition: all 0.4s linear;
-ms-transition: all 0.4s linear;
-webkit-transition: all 0.4s linear;
transition: all 0.4s linear;
text-align: center;
}
.cookie-button:hover {
background-color: #0072AE;
}
/* END COOKIE */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section id="cookie-consent" class="cookie-consent">
<div class="cookie-consent-inner">
<div class="cookie">
<div class="cookie-msg">We use cookies to track visits to our website, we store no personal details. By staying on the site you consent to our cookie policy.</div>
<div class="cookie-accept">
<button id="cookie" class="cookie-button" onclick='SetCookieAndHideDiv();'>OK</button>
</div>
</div>
</div>
</section>
I know this is very basic, but it has basic needs.
php jquery html cookies
2
The cookie isn't related to the user, but to the browser. Just set the cookie when the OK button is clicked.
– Rory McCrossan
Nov 23 '18 at 12:10
Cookies are stored client-side, in the browser. If a/the cookie exists, the user has already accepted (no need for you to show the consent dialog).
– kerbholz
Nov 23 '18 at 12:14
I appreciate you probably know what you're talking about. But these don't make any sense. @RoryMcCrossan
– Jason Is My Name
Nov 23 '18 at 12:22
@kerbholz - tag
– Jason Is My Name
Nov 23 '18 at 12:22
1
This is almost the same thing - stackoverflow.com/questions/21587098/…
– peeebeee
Nov 23 '18 at 13:50
|
show 7 more comments
I have been told to add a cookie to a site. The site doesn't retain any user information other than the amount of visits the site gets.
I therefore do not have any user data to assign the cookie to so cannot tell if the user has already accepted.
Keeping the answer as quick to implement as possible and using simple code is it possible to only show the cookie to users that haven't already acknowledged it?
Heres where I am at. 'a basic cookie policy that will show on every page load'. Please help me make it only show once per user.
$(document).ready(function(){
if(getCookie("HideCookie") != "true") {
$("#cookie-consent").css('display','block');
}
});
function SetCookieAndHideDiv(){
setCookie('HideCookie','true',1);
$("#cookie-consent").css('display','none');
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
/* COOKIE */
#cookie-consent {
display: none;
width:100%;
height: 100%;
position:fixed;
bottom:0px;
background-color: rgba(0,0,0,0.8);
z-index: 100000;
}
.cookie-consent-inner {
width:100%;
padding: 20px;
position:fixed;
bottom:0px;
background-color: rgba(0,74,119,1);
color: rgba(255,209,0,1);
}
.cookie {
width: 100%;
max-width: 1248px;
margin: 0 auto;
display: -webkit-flex;
-webkit-flex-wrap: wrap;
display: flex;
flex-wrap: wrap;
align-items: center;
}
.cookie-msg {
width: calc(100% - 120px);
margin-right: 20px;
float: left;
}
.cookie-accept {
width: 100px;
float: right;
}
.cookie-button {
font-size: 16px;
line-height: 40px;
padding: 0px;
color: #FFD100;
width: 100px;
background-color: #337ab7;
cursor: pointer;
border: 1px solid white!important;
-o-transition: all 0.4s linear;
-moz-transition: all 0.4s linear;
-ms-transition: all 0.4s linear;
-webkit-transition: all 0.4s linear;
transition: all 0.4s linear;
text-align: center;
}
.cookie-button:hover {
background-color: #0072AE;
}
/* END COOKIE */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section id="cookie-consent" class="cookie-consent">
<div class="cookie-consent-inner">
<div class="cookie">
<div class="cookie-msg">We use cookies to track visits to our website, we store no personal details. By staying on the site you consent to our cookie policy.</div>
<div class="cookie-accept">
<button id="cookie" class="cookie-button" onclick='SetCookieAndHideDiv();'>OK</button>
</div>
</div>
</div>
</section>
I know this is very basic, but it has basic needs.
php jquery html cookies
I have been told to add a cookie to a site. The site doesn't retain any user information other than the amount of visits the site gets.
I therefore do not have any user data to assign the cookie to so cannot tell if the user has already accepted.
Keeping the answer as quick to implement as possible and using simple code is it possible to only show the cookie to users that haven't already acknowledged it?
Heres where I am at. 'a basic cookie policy that will show on every page load'. Please help me make it only show once per user.
$(document).ready(function(){
if(getCookie("HideCookie") != "true") {
$("#cookie-consent").css('display','block');
}
});
function SetCookieAndHideDiv(){
setCookie('HideCookie','true',1);
$("#cookie-consent").css('display','none');
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
/* COOKIE */
#cookie-consent {
display: none;
width:100%;
height: 100%;
position:fixed;
bottom:0px;
background-color: rgba(0,0,0,0.8);
z-index: 100000;
}
.cookie-consent-inner {
width:100%;
padding: 20px;
position:fixed;
bottom:0px;
background-color: rgba(0,74,119,1);
color: rgba(255,209,0,1);
}
.cookie {
width: 100%;
max-width: 1248px;
margin: 0 auto;
display: -webkit-flex;
-webkit-flex-wrap: wrap;
display: flex;
flex-wrap: wrap;
align-items: center;
}
.cookie-msg {
width: calc(100% - 120px);
margin-right: 20px;
float: left;
}
.cookie-accept {
width: 100px;
float: right;
}
.cookie-button {
font-size: 16px;
line-height: 40px;
padding: 0px;
color: #FFD100;
width: 100px;
background-color: #337ab7;
cursor: pointer;
border: 1px solid white!important;
-o-transition: all 0.4s linear;
-moz-transition: all 0.4s linear;
-ms-transition: all 0.4s linear;
-webkit-transition: all 0.4s linear;
transition: all 0.4s linear;
text-align: center;
}
.cookie-button:hover {
background-color: #0072AE;
}
/* END COOKIE */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section id="cookie-consent" class="cookie-consent">
<div class="cookie-consent-inner">
<div class="cookie">
<div class="cookie-msg">We use cookies to track visits to our website, we store no personal details. By staying on the site you consent to our cookie policy.</div>
<div class="cookie-accept">
<button id="cookie" class="cookie-button" onclick='SetCookieAndHideDiv();'>OK</button>
</div>
</div>
</div>
</section>
I know this is very basic, but it has basic needs.
$(document).ready(function(){
if(getCookie("HideCookie") != "true") {
$("#cookie-consent").css('display','block');
}
});
function SetCookieAndHideDiv(){
setCookie('HideCookie','true',1);
$("#cookie-consent").css('display','none');
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
/* COOKIE */
#cookie-consent {
display: none;
width:100%;
height: 100%;
position:fixed;
bottom:0px;
background-color: rgba(0,0,0,0.8);
z-index: 100000;
}
.cookie-consent-inner {
width:100%;
padding: 20px;
position:fixed;
bottom:0px;
background-color: rgba(0,74,119,1);
color: rgba(255,209,0,1);
}
.cookie {
width: 100%;
max-width: 1248px;
margin: 0 auto;
display: -webkit-flex;
-webkit-flex-wrap: wrap;
display: flex;
flex-wrap: wrap;
align-items: center;
}
.cookie-msg {
width: calc(100% - 120px);
margin-right: 20px;
float: left;
}
.cookie-accept {
width: 100px;
float: right;
}
.cookie-button {
font-size: 16px;
line-height: 40px;
padding: 0px;
color: #FFD100;
width: 100px;
background-color: #337ab7;
cursor: pointer;
border: 1px solid white!important;
-o-transition: all 0.4s linear;
-moz-transition: all 0.4s linear;
-ms-transition: all 0.4s linear;
-webkit-transition: all 0.4s linear;
transition: all 0.4s linear;
text-align: center;
}
.cookie-button:hover {
background-color: #0072AE;
}
/* END COOKIE */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section id="cookie-consent" class="cookie-consent">
<div class="cookie-consent-inner">
<div class="cookie">
<div class="cookie-msg">We use cookies to track visits to our website, we store no personal details. By staying on the site you consent to our cookie policy.</div>
<div class="cookie-accept">
<button id="cookie" class="cookie-button" onclick='SetCookieAndHideDiv();'>OK</button>
</div>
</div>
</div>
</section>
$(document).ready(function(){
if(getCookie("HideCookie") != "true") {
$("#cookie-consent").css('display','block');
}
});
function SetCookieAndHideDiv(){
setCookie('HideCookie','true',1);
$("#cookie-consent").css('display','none');
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
/* COOKIE */
#cookie-consent {
display: none;
width:100%;
height: 100%;
position:fixed;
bottom:0px;
background-color: rgba(0,0,0,0.8);
z-index: 100000;
}
.cookie-consent-inner {
width:100%;
padding: 20px;
position:fixed;
bottom:0px;
background-color: rgba(0,74,119,1);
color: rgba(255,209,0,1);
}
.cookie {
width: 100%;
max-width: 1248px;
margin: 0 auto;
display: -webkit-flex;
-webkit-flex-wrap: wrap;
display: flex;
flex-wrap: wrap;
align-items: center;
}
.cookie-msg {
width: calc(100% - 120px);
margin-right: 20px;
float: left;
}
.cookie-accept {
width: 100px;
float: right;
}
.cookie-button {
font-size: 16px;
line-height: 40px;
padding: 0px;
color: #FFD100;
width: 100px;
background-color: #337ab7;
cursor: pointer;
border: 1px solid white!important;
-o-transition: all 0.4s linear;
-moz-transition: all 0.4s linear;
-ms-transition: all 0.4s linear;
-webkit-transition: all 0.4s linear;
transition: all 0.4s linear;
text-align: center;
}
.cookie-button:hover {
background-color: #0072AE;
}
/* END COOKIE */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section id="cookie-consent" class="cookie-consent">
<div class="cookie-consent-inner">
<div class="cookie">
<div class="cookie-msg">We use cookies to track visits to our website, we store no personal details. By staying on the site you consent to our cookie policy.</div>
<div class="cookie-accept">
<button id="cookie" class="cookie-button" onclick='SetCookieAndHideDiv();'>OK</button>
</div>
</div>
</div>
</section>
php jquery html cookies
php jquery html cookies
edited Nov 23 '18 at 14:38
Jason Is My Name
asked Nov 23 '18 at 12:09
Jason Is My NameJason Is My Name
5310
5310
2
The cookie isn't related to the user, but to the browser. Just set the cookie when the OK button is clicked.
– Rory McCrossan
Nov 23 '18 at 12:10
Cookies are stored client-side, in the browser. If a/the cookie exists, the user has already accepted (no need for you to show the consent dialog).
– kerbholz
Nov 23 '18 at 12:14
I appreciate you probably know what you're talking about. But these don't make any sense. @RoryMcCrossan
– Jason Is My Name
Nov 23 '18 at 12:22
@kerbholz - tag
– Jason Is My Name
Nov 23 '18 at 12:22
1
This is almost the same thing - stackoverflow.com/questions/21587098/…
– peeebeee
Nov 23 '18 at 13:50
|
show 7 more comments
2
The cookie isn't related to the user, but to the browser. Just set the cookie when the OK button is clicked.
– Rory McCrossan
Nov 23 '18 at 12:10
Cookies are stored client-side, in the browser. If a/the cookie exists, the user has already accepted (no need for you to show the consent dialog).
– kerbholz
Nov 23 '18 at 12:14
I appreciate you probably know what you're talking about. But these don't make any sense. @RoryMcCrossan
– Jason Is My Name
Nov 23 '18 at 12:22
@kerbholz - tag
– Jason Is My Name
Nov 23 '18 at 12:22
1
This is almost the same thing - stackoverflow.com/questions/21587098/…
– peeebeee
Nov 23 '18 at 13:50
2
2
The cookie isn't related to the user, but to the browser. Just set the cookie when the OK button is clicked.
– Rory McCrossan
Nov 23 '18 at 12:10
The cookie isn't related to the user, but to the browser. Just set the cookie when the OK button is clicked.
– Rory McCrossan
Nov 23 '18 at 12:10
Cookies are stored client-side, in the browser. If a/the cookie exists, the user has already accepted (no need for you to show the consent dialog).
– kerbholz
Nov 23 '18 at 12:14
Cookies are stored client-side, in the browser. If a/the cookie exists, the user has already accepted (no need for you to show the consent dialog).
– kerbholz
Nov 23 '18 at 12:14
I appreciate you probably know what you're talking about. But these don't make any sense. @RoryMcCrossan
– Jason Is My Name
Nov 23 '18 at 12:22
I appreciate you probably know what you're talking about. But these don't make any sense. @RoryMcCrossan
– Jason Is My Name
Nov 23 '18 at 12:22
@kerbholz - tag
– Jason Is My Name
Nov 23 '18 at 12:22
@kerbholz - tag
– Jason Is My Name
Nov 23 '18 at 12:22
1
1
This is almost the same thing - stackoverflow.com/questions/21587098/…
– peeebeee
Nov 23 '18 at 13:50
This is almost the same thing - stackoverflow.com/questions/21587098/…
– peeebeee
Nov 23 '18 at 13:50
|
show 7 more comments
1 Answer
1
active
oldest
votes
Thanks for all the comments - glad it got a bit of traction.
Please up-vote the question so others can get this code.
Working Code:
$(document).ready(function(){
if(getCookie("HideCookie") != "true") {
$("#cookie-consent").css('display','block');
}
});
function SetCookieAndHideDiv(){
setCookie('HideCookie','true',1);
$("#cookie-consent").css('display','none');
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
/* COOKIE */
#cookie-consent {
display: none;
width:100%;
height: 100%;
position:fixed;
bottom:0px;
background-color: rgba(0,0,0,0.8);
z-index: 100000;
}
.cookie-consent-inner {
width:100%;
padding: 20px;
position:fixed;
bottom:0px;
background-color: rgba(0,74,119,1);
color: rgba(255,209,0,1);
}
.cookie {
width: 100%;
max-width: 1248px;
margin: 0 auto;
display: -webkit-flex;
-webkit-flex-wrap: wrap;
display: flex;
flex-wrap: wrap;
align-items: center;
}
.cookie-msg {
width: calc(100% - 120px);
margin-right: 20px;
float: left;
}
.cookie-accept {
width: 100px;
float: right;
}
.cookie-button {
font-size: 16px;
line-height: 40px;
padding: 0px;
color: #FFD100;
width: 100px;
background-color: #337ab7;
cursor: pointer;
border: 1px solid white!important;
-o-transition: all 0.4s linear;
-moz-transition: all 0.4s linear;
-ms-transition: all 0.4s linear;
-webkit-transition: all 0.4s linear;
transition: all 0.4s linear;
text-align: center;
}
.cookie-button:hover {
background-color: #0072AE;
}
/* END COOKIE */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section id="cookie-consent" class="cookie-consent">
<div class="cookie-consent-inner">
<div class="cookie">
<div class="cookie-msg">We use cookies to track visits to our website, we store no personal details. By staying on the site you consent to our cookie policy.</div>
<div class="cookie-accept">
<button id="cookie" class="cookie-button" onclick='SetCookieAndHideDiv();'>OK</button>
</div>
</div>
</div>
</section>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446479%2fsimple-cookie-consent-how-to-assess-if-the-user-has-already-clicked-and-consen%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for all the comments - glad it got a bit of traction.
Please up-vote the question so others can get this code.
Working Code:
$(document).ready(function(){
if(getCookie("HideCookie") != "true") {
$("#cookie-consent").css('display','block');
}
});
function SetCookieAndHideDiv(){
setCookie('HideCookie','true',1);
$("#cookie-consent").css('display','none');
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
/* COOKIE */
#cookie-consent {
display: none;
width:100%;
height: 100%;
position:fixed;
bottom:0px;
background-color: rgba(0,0,0,0.8);
z-index: 100000;
}
.cookie-consent-inner {
width:100%;
padding: 20px;
position:fixed;
bottom:0px;
background-color: rgba(0,74,119,1);
color: rgba(255,209,0,1);
}
.cookie {
width: 100%;
max-width: 1248px;
margin: 0 auto;
display: -webkit-flex;
-webkit-flex-wrap: wrap;
display: flex;
flex-wrap: wrap;
align-items: center;
}
.cookie-msg {
width: calc(100% - 120px);
margin-right: 20px;
float: left;
}
.cookie-accept {
width: 100px;
float: right;
}
.cookie-button {
font-size: 16px;
line-height: 40px;
padding: 0px;
color: #FFD100;
width: 100px;
background-color: #337ab7;
cursor: pointer;
border: 1px solid white!important;
-o-transition: all 0.4s linear;
-moz-transition: all 0.4s linear;
-ms-transition: all 0.4s linear;
-webkit-transition: all 0.4s linear;
transition: all 0.4s linear;
text-align: center;
}
.cookie-button:hover {
background-color: #0072AE;
}
/* END COOKIE */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section id="cookie-consent" class="cookie-consent">
<div class="cookie-consent-inner">
<div class="cookie">
<div class="cookie-msg">We use cookies to track visits to our website, we store no personal details. By staying on the site you consent to our cookie policy.</div>
<div class="cookie-accept">
<button id="cookie" class="cookie-button" onclick='SetCookieAndHideDiv();'>OK</button>
</div>
</div>
</div>
</section>
add a comment |
Thanks for all the comments - glad it got a bit of traction.
Please up-vote the question so others can get this code.
Working Code:
$(document).ready(function(){
if(getCookie("HideCookie") != "true") {
$("#cookie-consent").css('display','block');
}
});
function SetCookieAndHideDiv(){
setCookie('HideCookie','true',1);
$("#cookie-consent").css('display','none');
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
/* COOKIE */
#cookie-consent {
display: none;
width:100%;
height: 100%;
position:fixed;
bottom:0px;
background-color: rgba(0,0,0,0.8);
z-index: 100000;
}
.cookie-consent-inner {
width:100%;
padding: 20px;
position:fixed;
bottom:0px;
background-color: rgba(0,74,119,1);
color: rgba(255,209,0,1);
}
.cookie {
width: 100%;
max-width: 1248px;
margin: 0 auto;
display: -webkit-flex;
-webkit-flex-wrap: wrap;
display: flex;
flex-wrap: wrap;
align-items: center;
}
.cookie-msg {
width: calc(100% - 120px);
margin-right: 20px;
float: left;
}
.cookie-accept {
width: 100px;
float: right;
}
.cookie-button {
font-size: 16px;
line-height: 40px;
padding: 0px;
color: #FFD100;
width: 100px;
background-color: #337ab7;
cursor: pointer;
border: 1px solid white!important;
-o-transition: all 0.4s linear;
-moz-transition: all 0.4s linear;
-ms-transition: all 0.4s linear;
-webkit-transition: all 0.4s linear;
transition: all 0.4s linear;
text-align: center;
}
.cookie-button:hover {
background-color: #0072AE;
}
/* END COOKIE */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section id="cookie-consent" class="cookie-consent">
<div class="cookie-consent-inner">
<div class="cookie">
<div class="cookie-msg">We use cookies to track visits to our website, we store no personal details. By staying on the site you consent to our cookie policy.</div>
<div class="cookie-accept">
<button id="cookie" class="cookie-button" onclick='SetCookieAndHideDiv();'>OK</button>
</div>
</div>
</div>
</section>
add a comment |
Thanks for all the comments - glad it got a bit of traction.
Please up-vote the question so others can get this code.
Working Code:
$(document).ready(function(){
if(getCookie("HideCookie") != "true") {
$("#cookie-consent").css('display','block');
}
});
function SetCookieAndHideDiv(){
setCookie('HideCookie','true',1);
$("#cookie-consent").css('display','none');
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
/* COOKIE */
#cookie-consent {
display: none;
width:100%;
height: 100%;
position:fixed;
bottom:0px;
background-color: rgba(0,0,0,0.8);
z-index: 100000;
}
.cookie-consent-inner {
width:100%;
padding: 20px;
position:fixed;
bottom:0px;
background-color: rgba(0,74,119,1);
color: rgba(255,209,0,1);
}
.cookie {
width: 100%;
max-width: 1248px;
margin: 0 auto;
display: -webkit-flex;
-webkit-flex-wrap: wrap;
display: flex;
flex-wrap: wrap;
align-items: center;
}
.cookie-msg {
width: calc(100% - 120px);
margin-right: 20px;
float: left;
}
.cookie-accept {
width: 100px;
float: right;
}
.cookie-button {
font-size: 16px;
line-height: 40px;
padding: 0px;
color: #FFD100;
width: 100px;
background-color: #337ab7;
cursor: pointer;
border: 1px solid white!important;
-o-transition: all 0.4s linear;
-moz-transition: all 0.4s linear;
-ms-transition: all 0.4s linear;
-webkit-transition: all 0.4s linear;
transition: all 0.4s linear;
text-align: center;
}
.cookie-button:hover {
background-color: #0072AE;
}
/* END COOKIE */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section id="cookie-consent" class="cookie-consent">
<div class="cookie-consent-inner">
<div class="cookie">
<div class="cookie-msg">We use cookies to track visits to our website, we store no personal details. By staying on the site you consent to our cookie policy.</div>
<div class="cookie-accept">
<button id="cookie" class="cookie-button" onclick='SetCookieAndHideDiv();'>OK</button>
</div>
</div>
</div>
</section>
Thanks for all the comments - glad it got a bit of traction.
Please up-vote the question so others can get this code.
Working Code:
$(document).ready(function(){
if(getCookie("HideCookie") != "true") {
$("#cookie-consent").css('display','block');
}
});
function SetCookieAndHideDiv(){
setCookie('HideCookie','true',1);
$("#cookie-consent").css('display','none');
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
/* COOKIE */
#cookie-consent {
display: none;
width:100%;
height: 100%;
position:fixed;
bottom:0px;
background-color: rgba(0,0,0,0.8);
z-index: 100000;
}
.cookie-consent-inner {
width:100%;
padding: 20px;
position:fixed;
bottom:0px;
background-color: rgba(0,74,119,1);
color: rgba(255,209,0,1);
}
.cookie {
width: 100%;
max-width: 1248px;
margin: 0 auto;
display: -webkit-flex;
-webkit-flex-wrap: wrap;
display: flex;
flex-wrap: wrap;
align-items: center;
}
.cookie-msg {
width: calc(100% - 120px);
margin-right: 20px;
float: left;
}
.cookie-accept {
width: 100px;
float: right;
}
.cookie-button {
font-size: 16px;
line-height: 40px;
padding: 0px;
color: #FFD100;
width: 100px;
background-color: #337ab7;
cursor: pointer;
border: 1px solid white!important;
-o-transition: all 0.4s linear;
-moz-transition: all 0.4s linear;
-ms-transition: all 0.4s linear;
-webkit-transition: all 0.4s linear;
transition: all 0.4s linear;
text-align: center;
}
.cookie-button:hover {
background-color: #0072AE;
}
/* END COOKIE */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section id="cookie-consent" class="cookie-consent">
<div class="cookie-consent-inner">
<div class="cookie">
<div class="cookie-msg">We use cookies to track visits to our website, we store no personal details. By staying on the site you consent to our cookie policy.</div>
<div class="cookie-accept">
<button id="cookie" class="cookie-button" onclick='SetCookieAndHideDiv();'>OK</button>
</div>
</div>
</div>
</section>
$(document).ready(function(){
if(getCookie("HideCookie") != "true") {
$("#cookie-consent").css('display','block');
}
});
function SetCookieAndHideDiv(){
setCookie('HideCookie','true',1);
$("#cookie-consent").css('display','none');
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
/* COOKIE */
#cookie-consent {
display: none;
width:100%;
height: 100%;
position:fixed;
bottom:0px;
background-color: rgba(0,0,0,0.8);
z-index: 100000;
}
.cookie-consent-inner {
width:100%;
padding: 20px;
position:fixed;
bottom:0px;
background-color: rgba(0,74,119,1);
color: rgba(255,209,0,1);
}
.cookie {
width: 100%;
max-width: 1248px;
margin: 0 auto;
display: -webkit-flex;
-webkit-flex-wrap: wrap;
display: flex;
flex-wrap: wrap;
align-items: center;
}
.cookie-msg {
width: calc(100% - 120px);
margin-right: 20px;
float: left;
}
.cookie-accept {
width: 100px;
float: right;
}
.cookie-button {
font-size: 16px;
line-height: 40px;
padding: 0px;
color: #FFD100;
width: 100px;
background-color: #337ab7;
cursor: pointer;
border: 1px solid white!important;
-o-transition: all 0.4s linear;
-moz-transition: all 0.4s linear;
-ms-transition: all 0.4s linear;
-webkit-transition: all 0.4s linear;
transition: all 0.4s linear;
text-align: center;
}
.cookie-button:hover {
background-color: #0072AE;
}
/* END COOKIE */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section id="cookie-consent" class="cookie-consent">
<div class="cookie-consent-inner">
<div class="cookie">
<div class="cookie-msg">We use cookies to track visits to our website, we store no personal details. By staying on the site you consent to our cookie policy.</div>
<div class="cookie-accept">
<button id="cookie" class="cookie-button" onclick='SetCookieAndHideDiv();'>OK</button>
</div>
</div>
</div>
</section>
$(document).ready(function(){
if(getCookie("HideCookie") != "true") {
$("#cookie-consent").css('display','block');
}
});
function SetCookieAndHideDiv(){
setCookie('HideCookie','true',1);
$("#cookie-consent").css('display','none');
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
/* COOKIE */
#cookie-consent {
display: none;
width:100%;
height: 100%;
position:fixed;
bottom:0px;
background-color: rgba(0,0,0,0.8);
z-index: 100000;
}
.cookie-consent-inner {
width:100%;
padding: 20px;
position:fixed;
bottom:0px;
background-color: rgba(0,74,119,1);
color: rgba(255,209,0,1);
}
.cookie {
width: 100%;
max-width: 1248px;
margin: 0 auto;
display: -webkit-flex;
-webkit-flex-wrap: wrap;
display: flex;
flex-wrap: wrap;
align-items: center;
}
.cookie-msg {
width: calc(100% - 120px);
margin-right: 20px;
float: left;
}
.cookie-accept {
width: 100px;
float: right;
}
.cookie-button {
font-size: 16px;
line-height: 40px;
padding: 0px;
color: #FFD100;
width: 100px;
background-color: #337ab7;
cursor: pointer;
border: 1px solid white!important;
-o-transition: all 0.4s linear;
-moz-transition: all 0.4s linear;
-ms-transition: all 0.4s linear;
-webkit-transition: all 0.4s linear;
transition: all 0.4s linear;
text-align: center;
}
.cookie-button:hover {
background-color: #0072AE;
}
/* END COOKIE */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section id="cookie-consent" class="cookie-consent">
<div class="cookie-consent-inner">
<div class="cookie">
<div class="cookie-msg">We use cookies to track visits to our website, we store no personal details. By staying on the site you consent to our cookie policy.</div>
<div class="cookie-accept">
<button id="cookie" class="cookie-button" onclick='SetCookieAndHideDiv();'>OK</button>
</div>
</div>
</div>
</section>
answered Nov 23 '18 at 14:40
Jason Is My NameJason Is My Name
5310
5310
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446479%2fsimple-cookie-consent-how-to-assess-if-the-user-has-already-clicked-and-consen%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
The cookie isn't related to the user, but to the browser. Just set the cookie when the OK button is clicked.
– Rory McCrossan
Nov 23 '18 at 12:10
Cookies are stored client-side, in the browser. If a/the cookie exists, the user has already accepted (no need for you to show the consent dialog).
– kerbholz
Nov 23 '18 at 12:14
I appreciate you probably know what you're talking about. But these don't make any sense. @RoryMcCrossan
– Jason Is My Name
Nov 23 '18 at 12:22
@kerbholz - tag
– Jason Is My Name
Nov 23 '18 at 12:22
1
This is almost the same thing - stackoverflow.com/questions/21587098/…
– peeebeee
Nov 23 '18 at 13:50