0

Logically this seems to be correct. However, either the setCookie or getCookie functions simply aren't firing?

cookie.js

function setCookie(c_name,value,exdays) { var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays); var c_value=escape(value) +
((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value; }

function getCookie(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
  {
  c_start = c_value.indexOf(c_name + "=");
  }
if (c_start == -1)
  {
  c_value = null;
  }
else
  {
  c_start = c_value.indexOf("=", c_start) + 1;
  var c_end = c_value.indexOf(";", c_start);
  if (c_end == -1)
  {
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
}

index.php

var newCookie = parseInt(getCookie("liked_count"));
if(newCookie != null && newCookie != ""){
newCookie += 1;
setCookie("liked_count",newCookie,5);
}else{
setCookie("liked_count",1,5);
}

No matter which side of the if statement it follows, no cookie is set regardless. From what I can tell there are no errors or warnings, so could it be that it cannot find the setCookie and getCookie function inside the my cookies.js file?

The cookies.js file successfully locates, so I'm at my wits end here.

<head>
<script type="text/javascript" src="assets/js/cookies.js"></script>
</head>

Any help would be much appreciated!

EDIT:

Oh sorry, this is embarrassing... It turns out that the cookie.js file was being cached and I had actually moved file location. It was that simple. Sorry for this waste of time!

5
  • The easiest way to tell if functions are firing is to have them output something to the console using console.log. You could also alert, but that's a bit more obtrusive! Commented Oct 27, 2013 at 16:34
  • Nothing shows up in console.log, but when I inspect element in-browser the code is shown to be there. Commented Oct 27, 2013 at 16:39
  • Could you put this up in a jsFiddle? Commented Oct 27, 2013 at 16:42
  • Oh sorry, this is embarrassing... It turns out that the cookie.js file was being cached and I had actually moved file location. It was that simple. Sorry for this waste of time! Commented Oct 27, 2013 at 16:43
  • I think you're still going to have a problem, you should take a look at my answer. Commented Oct 27, 2013 at 16:44

1 Answer 1

2

The problem you're having is with your use of:

var newCookie = parseInt(getCookie("liked_count"));

MDN parseInt Documentation

parseInt returns NaN if it fails. Instead of the following line:

if(newCookie != null && newCookie != ""){

you should have

if(!isNaN(newCookie)) {

http://plnkr.co/edit/2Nj5pj

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

1 Comment

It still worked regardless, but this way is cleaner and likely more compatible. Thank you!

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.