views:

28937

answers:

7

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 info that is nicer than the default JS apis?
+48  A: 

See here: http://plugins.jquery.com/project/cookie

Alex Fort
omg's you googles faster than I!
iAn
does anyone have a mirror? the site is down
Zachary Burt
Having the same proble here. I found a link to an improved version here: http://jquery-howto.blogspot.com/2010/09/jquery-cookies-getsetdelete-plugin.html
jQuery Lover
good improvement...
CallMeLaNN
+4  A: 

Take a look at the Cookie Plugin for JQuery

iAn
+3  A: 

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.

SeanDowney
+7  A: 

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"

Steve Moyer
+6  A: 

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

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
+13  A: 

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

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

So, I don't know how much that helps answer your question, but I do know that I would never load a library to help me with that. There are instances where an abstraction layer is not necessary. In this case, the abstraction becomes more hazardous/bloated then the benefits warrant. IMO, of course.

adam
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
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
Not related to jQuery as asked. Peter Ajtai comment shows why casademora ask for jQuery plugin instead of Javascript.
CallMeLaNN
+2  A: 

Just replied to Zachary Burt's commented. It seems jQuery cookie plugin is not available for download

However, you can download the same jQuery Cookie plugin with some improvements described here.

jQuery Lover
I also realize, the cookie plugin development stopped there since the first release and not sure whether it compatible with jQuery 1.4*
CallMeLaNN
The plugin is compatible with jQuery 1.4, since the extending syntax has not changed since then...
jQuery Lover