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.
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.
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.
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.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With