Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

getCookie not working?

cookies cookie javascript help getcookie

Best Answer elliottveares, 10 April 2014 - 03:43 PM

Solved it, did not realise you had to separate all the cookies out first, then get a cookies value.

 

Modified working code below. (Alert statements just for testing, processing would happen here)

window.onload = function()
{	

//Sorts the cookies out, seperated by semicolons

function getCookie(cookiename) 
  {
  // Get name followed by anything except a semicolon
  var cookiestring=RegExp(""+cookiename+"[^;]+").exec(document.cookie);
  // Return everything after the equal sign
  return unescape(!!cookiestring ? cookiestring.toString().replace(/^[^=]+./,"") : "");
  }


var Valid = getCookie("Cookie_Accepted"); //should declare Valid and set it to the value of Cookie_Accepted

if (Valid=="Yes")
  {
  alert ("Cookie is set");
  
  }
else 
  {
 document.cookie="Cookie_Accepted=Yes";
 
   alert ("Cookie is not set"); 
  }
}




Go to the full post


This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1 elliottveares

elliottveares

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 56 posts

Posted 10 April 2014 - 02:17 PM

To comply with the EU cookie law, i have to display a warning about cookies and thus wrote this script but it is not working?

 

Script is self explanatory, but can't work out why it is not working.

window.onload = function()
{	
var Valid = getCookie("Cookie_Accepted"); //should declare Valid variable and set it to the value                                               of Cookie_Accepted
if (Valid=="Yes")
  {
  alert ("Cookie is set");
  
  }
else 
  {
 document.cookie="Cookie_Accepted=Yes";
 
   alert ("Cookie is not set"); 
  }
}





Regards: Elliott



#2 elliottveares

elliottveares

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 56 posts

Posted 10 April 2014 - 03:43 PM   Best Answer

Solved it, did not realise you had to separate all the cookies out first, then get a cookies value.

 

Modified working code below. (Alert statements just for testing, processing would happen here)

window.onload = function()
{	

//Sorts the cookies out, seperated by semicolons

function getCookie(cookiename) 
  {
  // Get name followed by anything except a semicolon
  var cookiestring=RegExp(""+cookiename+"[^;]+").exec(document.cookie);
  // Return everything after the equal sign
  return unescape(!!cookiestring ? cookiestring.toString().replace(/^[^=]+./,"") : "");
  }


var Valid = getCookie("Cookie_Accepted"); //should declare Valid and set it to the value of Cookie_Accepted

if (Valid=="Yes")
  {
  alert ("Cookie is set");
  
  }
else 
  {
 document.cookie="Cookie_Accepted=Yes";
 
   alert ("Cookie is not set"); 
  }
}









Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download