1
<script type="text/javascript">
function WorldwideSellingModelCookie(){
   days=7;
   myDate = new Date();
   myDate.setTime(myDate.getTime()+(days*24*60*60*1000));
   document.cookie = 'WorldwideSellingModelCookie=Accepted; expires=' + myDate.toGMTString();
}
function CheckCookies(){  
    var worldwideSellingCookie = getCookie("WorldwideSellingModelCookie"); 
    if (worldwideSellingCookie == "Accepted")
    {
        jQuery(".alert-worldwide").hide();
    } 
}
CheckCookies();
</script>

Hi,

The cookie is being created, I am just unsure how to get my if statement to work within my CheckCookies function so that it hide a div on the page?

I am recieving the following console error:

Uncaught ReferenceError: getCookie is not defined

Can anyone advise what I am doing wrong?

Thanks

UPDATE:

<script type="text/javascript">
function WorldwideSellingModelCookie(){
   days=7;
   myDate = new Date();
   myDate.setTime(myDate.getTime()+(days*24*60*60*1000));
   document.cookie = 'WorldwideSellingModelCookie=Accepted; expires=' + myDate.toGMTString();
}

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 CheckCookies(){  
    var worldwideSellingCookie = getCookie("WorldwideSellingModelCookie"); 
    if (worldwideSellingCookie == "Accepted")
    {
        jQuery(".alert-worldwide").hide();
    } 
}
CheckCookies();
</script>

Just added the function to check the cookie and seems to be working now, however the div appears for a split second before hiding. Is there any way to stop this from happening?

5
  • There is Jquery plugin called jquery-cookie Commented Oct 18, 2016 at 15:34
  • Have you tried this syntax? w3schools.com/js/js_cookies.asp use .toUTCString() add a path=/ Commented Oct 18, 2016 at 15:36
  • 2
    Where have you defined the getCookie() function? Commented Oct 18, 2016 at 15:37
  • @RoryMcCrossan That must be the issue, I haven't.. Commented Oct 18, 2016 at 15:39
  • 1
    There's no way to stop the div appearing for an instant. The delay is the time it takes for the DOM to be loaded and the JS to read the cookie. The alternative is to flip the logic so that it's hidden by default in CSS then gets shown if the cookie != 'Accepted' Commented Oct 18, 2016 at 15:57

1 Answer 1

6

These are the functions I use to add, get, or clear cookies in JavaScript.

/* function creates cooke with random key */
function setCookie(inputs) {
  /* cookie name */
  var name = (inputs[0]) ? inputs[0] : "key" + document.cookie.length;
  /* cookie expire in 120 seconds */
  var date = new Date();
  date.setTime(date.getTime() + (120 * 1000));
  var expires = "; expires=" + date.toGMTString();
  /* sets cookie */
  document.cookie = name + "=" + inputs[1] + expires;
};

/* get the cookie based on input */
function getCookie(input) {
  var cookies = document.cookie.split(';');
  for (var i = 0; i < cookies.length; i++) {
    var name = cookies[i].split('=')[0].toLowerCase();
    var value = cookies[i].split('=')[1].toLowerCase();
    if (name === input) {
      return value;
    } else if (value === input) {
      return name;
    }
  }
  return "";
};

/* destroy me cookies (well only delete javascript cookies) */
function clearCookies(elements) {
  var cookies = document.cookie.split(';');
  for (var i = 0; i < cookies.length; i++) {
    var cookie = cookies[i];
    var eqPos = cookie.indexOf('=');
    var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
    document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
  }
  $(elements[0]).val('');
  $(elements[1]).val('');
  $(elements[2]).val('');
  $(elements[3]).html('No Cookie');
};

Also this is a good Document.cookie MDN

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.