Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can jQuery read/write cookies to a browser?

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?
like image 865
casademora Avatar asked Sep 18 '08 18:09

casademora


People also ask

Can jQuery create cookie?

Cookies can be set in the browser with the help of JavaScript or the jQuery. Here we will be seeing how to set cookies in the browser with the help of jQuery and how to remove them later on.

Can JavaScript read cookies?

JavaScript can also manipulate cookies using the cookie property of the Document object. JavaScript can read, create, modify, and delete the cookies that apply to the current web page.

Can JavaScript read third party cookies?

For security reasons, the cookie is accepted only if the server is a member of the domain specified by the domain string. If foo.com sent a cookie which had the domain name of bar.com , or even .com , then JavaSCript code on bar.com could read that cookie.


1 Answers

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') 
like image 163
adam Avatar answered Sep 25 '22 19:09

adam