-1

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
NullPointerException
  • 36,107
  • 79
  • 222
  • 382
  • Are you sure you are defining the functions in the global scope, not inside some other function? And are you getting any errors in console? – Michał Perłakowski Feb 20 '16 at 11:54
  • Do you happen to try this using the file:/// protocol? Cookies are not set when browsing using the file:/// protocol, depending on the browser. – reinder Feb 20 '16 at 12:10
  • @Gothdo the functions are stored in a .js file added this way in the header of the html file: – NullPointerException Feb 20 '16 at 12:25
  • @reinder i don't understand what you mean, i'm newbie in html css js, just starting. Not sure what are you talking about – NullPointerException Feb 20 '16 at 12:25
  • if i try the functions in chrome console, createCookie("cookiesPolicyAccepted", "true",3) writes undefined in the console and readCookie("cookiesPolicyAccepted") writes null in the console. Not sure if this is normal – NullPointerException Feb 20 '16 at 12:27
  • reinder meant if are accessing the page as a local file (file:// protocol), or from some server (http:// protocol). – Michał Perłakowski Feb 20 '16 at 12:28
  • it is a local page for now, it is still not upoloaded into a repository. Should i make a different implementation for testing in Windows Operating System local html website? Will my current implementation work when i upload it into a server? – NullPointerException Feb 20 '16 at 12:29

1 Answers1

1

You generally can't set set cookies in files accessed through file:// protocol (i.e. local files). You have to use a local server.

See also Setting Cookies using JavaScript in a local html file.

Community
  • 1
  • 1
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177