2

I've been searching for some JavaScript functions that would provide some sort of friendly interface to browser cookies, like setCookie(name, value, expireDays) and getCookie(name), but the top results Google brings up are related to code that is either considerably old and got no usage experience comments on it (quirksmode.org), desperately buggy (w3schools.com), used to contain serious bugs undiscovered for years (techpatterns.com), or only gives you one half of the answer (stackoverflow.com on its page at Javascript getCookie functions).

So the question is: after all the time cookies and JavaScript have been in touch, what functions would you suggest to be the most reliable ones when it comes to writing and reading browser cookies?

Community
  • 1
  • 1
Desmond Hume
  • 8,037
  • 14
  • 65
  • 112
  • 3
    _If_ you're using jQuery, the jQuery [cookie plugin](https://github.com/carhartl/jquery-cookie) is very simple to use and cross browser compatible. – Bojangles Dec 11 '11 at 00:03
  • 1
    Your question is awkward. Lack of info on language you are using. Only tagged as 'javascript' You are aware that users browsers can choose to not have scripting so this may be a poor solution for cookies? – thejartender Dec 11 '11 at 00:07
  • Could you clarify why you are opposed to `setCookie()` and `getCookie()`? – Allen Z. Dec 11 '11 at 00:10
  • I'm a happy user of the quirksmode version. Had no problems with it so far. – kapa Dec 11 '11 at 00:10
  • @JamWaffles Sure thing, jQuery could be the ultimate answer to plenty of JavaScript questions, including this one, but it's not fast-food style solutions that make this site so interesting. – Desmond Hume Dec 11 '11 at 00:18
  • @Desmond Before suffocating in your prejudice, have you tried looking at the source code of that fast food? – kapa Dec 11 '11 at 00:22
  • @bazmegakapa No because using jQuery's source code would require some license/copyright considerations (http://jquery.org/license/) – Desmond Hume Dec 11 '11 at 00:32
  • 1
    @DesmondHume Like what? (And the comment was to "look at it", not "copy it". – Dave Newton Dec 11 '11 at 00:35
  • 1
    @JamWaffles there is no such thing as _the_ jQuery cookie plugin. The one to which you refer is the most widely distributed, but there are a few. I am not even sure why the author of that one puts his code in the jQ namespace, or calls it a plugin considering it does not use any jQuery within and doesn't actually provide any jQuery-like functionality. – JAAulde Dec 11 '11 at 00:36
  • @JAAulde Bad choice of words. `This` would have worked better. If you take a minute to look at [jquery.cookie.js](https://github.com/carhartl/jquery-cookie/blob/master/jquery.cookie.js), the first line is `jQuery.cookie`, which uses jQuery. – Bojangles Dec 11 '11 at 00:37
  • @JAAulde Basically the only jQuery thing it uses is `.extend()`. But given how widespread jQuery is, it might be worth not to replicate this one functionality. – kapa Dec 11 '11 at 00:40
  • @bazmegakapa yeah, you're right, it does use jQ a bit. – JAAulde Dec 11 '11 at 00:42
  • @desmond-hume, check this out - http://thealjey.tumblr.com/javascript-cookies – Eugene Kuzmenko Jan 30 '15 at 13:01

1 Answers1

2

I maintain a JavaScript library for cookie manipulation which does not require jQuery. If jQuery is present, it does provide some bindings for it and aliases itself in the jQ namespace, but jQ is not needed.

For the time being both the lib and the jQuery add-ons are in the same file, but I am working on a new release which will split the two sections of code apart so that those who do not use jQuery can run a lighter weight version.

I have released it under a number of licenses, and if those don't work for you let me know and I will see what I can do about making it more comfortable for you .

JAAulde
  • 19,250
  • 5
  • 52
  • 63
  • 1
    I was just in the process of posting a link to your library when you posted this :) We used an older version of the library at HuffPost for some time. You've added a few things since, but it still looks pretty lightweight. – Ben Regenspan Dec 11 '11 at 00:34
  • @Ben Dare I ask why you stopped using it? Thanks, Jim – JAAulde Dec 11 '11 at 02:40
  • that was unclearly-written of me, HuffPost continues to use it. – Ben Regenspan Dec 11 '11 at 03:34
  • @Ben Oh cool, thanks for the clarification! I do see it on there now--under the older namespace. I'm excited to learn of this. :) – JAAulde Dec 11 '11 at 13:00