I'm trying to store a cookie when user has accepted a cookies policy on my website, so when it is accepted, the div created specifically to accept the policy must be hidden next time user opens the website or when the user press F5, the problem is that something is going wrong because the div is not being hidden.
This is the div code:
<div id="cookiesBar">
<div class="inner">
Solicitamos su permiso para obtener datos estadísticos de su navegación en esta web, en cumplimiento del Real
Decreto-ley 13/2012. Si continúa navegando consideramos que acepta el uso de cookies.
<a href="javascript:void(0);" class="ok" onclick="acceptCookiesPolicy();"><b>OK</b></a> |
<a href="http://politicadecookies.com" target="_blank" class="info">Más información</a>
</div>
</div>
To decide if showing or not that div i have this onload event at body declaration:
<body onload="checkIfCookiesPolicyIsAccepted();">
And this is my javascript file:
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
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,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
function acceptCookiesPolicy(){
createCookie("cookiesPolicyAccepted", "true", 365);
document.getElementById("cookiesBar").style.display="none";
}
function checkIfCookiesPolicyIsAccepted(){
if(readCookie("cookiesPolicyAccepted")!="true"){
document.getElementById("cookiesBar").style.display="block";
window.alert("el cookie NO es true");
}else{
document.getElementById("cookiesBar").style.display="none";
window.alert("el cookie es true");
}
}
As you can see, I added a window.alert
in checkIfCookiesPolictyIsAccepted
method and it is showing always the same alert with the same text "el cookie NO es true", so it is always reading a null or different from "true" value, so it is not working the cookie store/retrieve system.
I got this cookie store/retrieve system from here: http://www.quirksmode.org/js/cookies.html
What is wrong in my code?