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.
This question is related to
javascript
jquery
cookies
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.
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"
Take a look at the Cookie Plugin for jQuery.
A new jQuery plugin for cookie retrieval and manipulation with binding for forms, etc: http://plugins.jquery.com/project/cookies
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).
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"
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.
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"
A new jQuery plugin for cookie retrieval and manipulation with binding for forms, etc: http://plugins.jquery.com/project/cookies
Take a look at the Cookie Plugin for jQuery.
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.
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.
A new jQuery plugin for cookie retrieval and manipulation with binding for forms, etc: http://plugins.jquery.com/project/cookies
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.
Take a look at the Cookie Plugin for jQuery.
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.
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"
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.
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).
A new jQuery plugin for cookie retrieval and manipulation with binding for forms, etc: http://plugins.jquery.com/project/cookies
Take a look at the Cookie Plugin for jQuery.
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')
Source: Stackoverflow.com