简体   繁体   中英

How to read cookie value with javascript?

<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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM