1

This getCookie function code is from https://www.w3schools.com/js/js_cookies.asp

function getCookie(cname) {
  var name = cname + "=";
  var decodedCookie = decodeURIComponent(document.cookie);
  var ca = decodedCookie.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 "";
}

The part which I cannot understand easily is decodedCookie

var decodedCookie = decodeURIComponent(document.cookie);

I've already searched for articles about decodeURIComponent, and It decodes the encodedURIComponent.

But since theres no encoded cookie, why decodeURIComponent(document.cookie) needed?

function setCookie(cname, cvalue, exdays) {
  var d = new Date();
  d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
  var expires = "expires="+ d.toUTCString();
  document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

This is the code of setcookie function from w3schools, and they didnt set the cookie withencodeURIComponent`.

So... why?

I need some detailed explanation! Thanks!

DunDev
  • 210
  • 2
  • 13
jwkoo
  • 2,393
  • 5
  • 22
  • 35
  • 1
    It just looks like horrible code. Not that you wouldn't expect this on w3schools… There are [many](https://stackoverflow.com/q/14573223/1048572) other approaches, e.g. [this one](https://stackoverflow.com/a/38699214/1048572) – Bergi Nov 21 '17 at 00:28
  • It's a matter of Googling the correct things... running `encodeUriComponent` basically escapes a string so it won't break any of your code. `decodeUriComponent` reverses it. – LMulvey Nov 21 '17 at 00:30
  • @LMulvey Except that there is no `encodeURIComponent` call in the code. – Bergi Nov 21 '17 at 00:31
  • @Bergi horrible code? can you tell me why you would say that? I`ve searched for getcookie function and many people suggested to use the getcookie function of w3school. – jwkoo Nov 21 '17 at 00:31
  • @Bergi Do I have to use another get/set cookie function? – jwkoo Nov 21 '17 at 00:32
  • @jwkoo The stripping of the blanks and the check whether `c` begins with `name` *are* horrible. The apparently missing `encodeURIComponent` is at least suspicious. w3schools being popular is unfortunate, but says nothing about its quality. I'd recommend to use a function that you feel comfortable with, where you understand what it is doing and why. – Bergi Nov 21 '17 at 00:35
  • @Bergi fair point. It's awful code. – LMulvey Nov 21 '17 at 00:37
  • @jwkoo If you're just starting out and wanting to learn how to work with cookies, this tutorial may explain things better: https://www.tutorialspoint.com/javascript/javascript_cookies.htm – LMulvey Nov 21 '17 at 00:38
  • @Bergi really appreciate for your advice. – jwkoo Nov 21 '17 at 00:41
  • @Bergi Can I ask one more question? so If I need some code(like setcookie) to implement in my app, then rather googling the code in w3school, do I have to utilize stackoverflow? – jwkoo Nov 21 '17 at 00:43
  • @LMulvey appreciate it. thanks for your advice – jwkoo Nov 21 '17 at 00:44
  • can I replace the get cookie from w3schools with this function I made or it wrong ? function getcookie(cookie) { let decodedCookie = decodeURIComponent(document.cookie); let cookieslist = decodedCookie.split(cookie+"="); return cookieslist[1].split(";")[0].trim(); } – Mahmoud Magdy Aug 01 '21 at 04:22

1 Answers1

1

It is already mentioned in the tutorial: Decode the cookie string, to handle cookies with special characters, e.g. '$'

But since we do not have '$' then you can remove it with no effect here.

MAK
  • 11
  • 2