99

Simple example: I want to have some items on a page (like divs or table rows), and I want to let the user click on them to select them. That seems easy enough in jQuery. To save which items a user clicks on with no server-side post backs, I was thinking a cookie would be a simple way to get this done.

  1. Is this assumption that a cookie is OK in this case, correct?
  2. If it is correct, does the jQuery API have some way to read/write cookie information that is nicer than the default JavaScript APIs?
a--m
  • 4,716
  • 1
  • 39
  • 59
casademora
  • 67,775
  • 17
  • 69
  • 78

8 Answers8

53

The default JavaScript "API" for setting a cookie is as easy as:

document.cookie = 'mycookie=valueOfCookie;expires=DateHere;path=/'

Use the jQuery cookie plugin like:

$.cookie('mycookie', 'valueOfCookie')
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
adam
  • 3,888
  • 2
  • 20
  • 15
  • 14
    Yeah, writing cookies is easy, but reading them is sort of a pain, since you have to split strings and stuff. If you're already using JQuery, then the cookie plugin could be nice...... An annoying thing about reading cookies is that some browsers remove the final semicolon and some don't.... nice to have someone else handle all that. – Peter Ajtai Jun 12 '10 at 21:24
  • 8
    Oh, and the JQuery cookie plugin is only 40 lines of JS... and you can edit it to your desire, so that you don't feel like you're entering the abstraction danger zone. – Peter Ajtai Jun 12 '10 at 21:36
  • 3
    Not related to jQuery as asked. Peter Ajtai comment shows why casademora ask for jQuery plugin instead of Javascript. – CallMeLaNN Sep 30 '10 at 03:02
  • 4
    Here's the up-to-date link for the people ending up at dead links or malfunctioning plugins.jquery.com site: https://github.com/carhartl/jquery-cookie – Wiebe Tijsma May 01 '12 at 14:01
18

You'll need the cookie plugin, which provides several additional signatures to the cookie function.

$.cookie('cookie_name', 'cookie_value') stores a transient cookie (only exists within this session's scope, while $.cookie('cookie_name', 'cookie_value', 'cookie_expiration") creates a cookie that will last across sessions - see http://www.stilbuero.de/2006/09/17/cookie-plugin-for-jquery/ for more information on the JQuery cookie plugin.

If you want to set cookies that are used for the entire site, you'll need to use JavaScript like this:

document.cookie = "name=value; expires=date; domain=domain; path=path; secure"
Stefan
  • 14,530
  • 4
  • 55
  • 62
Steve Moyer
  • 5,663
  • 1
  • 24
  • 34
11

A new jQuery plugin for cookie retrieval and manipulation with binding for forms, etc: http://plugins.jquery.com/project/cookies

  • 3
    For anyone who reads the link too quickly, notice the 's' at the end of cookie to make this a different answer than the one by Alex Fort – Patrick Aug 13 '09 at 17:32
  • 2
    Here's a better link at the time of writing https://github.com/carhartl/jquery-cookie – Wiebe Tijsma May 01 '12 at 14:03
  • 1
    @Zidad that is not the same plugin as linked by Giver Of Cookies. The one he linked is now at http://code.google.com/p/cookies/ – JAAulde Jul 24 '12 at 18:58
  • @JAAulde Ah I didn't realize that. Because the link was dead :) Thanks! – Wiebe Tijsma Jul 31 '12 at 15:31
7

To answer your question, yes. The other have answered that part, but it also seems like you're asking if that's the best way to do it.

It would probably depend on what you are doing. Typically you would have a user click what items they want to buy (ordering for example). Then they would hit a buy or checkout button. Then the form would send off to a page and process the result. You could do all of that with a cookie but I would find it to be more difficult.

You may want to consider posting your second question in another topic.

John Topley
  • 113,588
  • 46
  • 195
  • 237
SeanDowney
  • 17,368
  • 20
  • 81
  • 90
5

Take a look at the Cookie Plugin for jQuery.

PJunior
  • 2,649
  • 1
  • 33
  • 29
Ian
  • 4,208
  • 21
  • 33
4

It seems the jQuery cookie plugin is not available for download. However, you can download the same jQuery cookie plugin with some improvements described in jQuery & Cookies (get/set/delete & a plugin).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jQuery Lover
  • 871
  • 1
  • 7
  • 8
  • I also realize, the cookie plugin development stopped there since the first release and not sure whether it compatible with jQuery 1.4* – CallMeLaNN Sep 30 '10 at 03:03
  • The plugin is compatible with jQuery 1.4, since the extending syntax has not changed since then... – jQuery Lover Oct 03 '10 at 23:01
4

You can browse all the jQuery plugins tagged with "cookie" here:

http://plugins.jquery.com/plugin-tags/cookies

Plenty of options there.

Check out the one called jQuery Storage, which takes advantage of HTML5's localStorage. If localStorage isn't available, it defaults to cookies. However, it doesn't allow you to set expiration.

Marshall Æon
  • 407
  • 1
  • 5
  • 18
2

I have managed to write a script allowing the user to choose his/her language, using the cookie script from Klaus Hartl. It took me a few hours work, and I hope I can help others.

Demodave
  • 6,242
  • 6
  • 43
  • 58
Porta Shqipe
  • 766
  • 7
  • 8