How to create, read and delete cookies using jQuery
jQuery cookie plugin is popular plugin, which is used to accomplish cookies tasks but one of the problem with this plugin is that it doesn't support JSON cookies. So what is the solution?
Dough is an easy to use cookie plugin for jQuery with powerful features. Dough can auto set your domain name with ‘.’ prefix so your cookies work with subdomains and will allow you to easily go from local to staging to product servers with out a problem.
Create Cookie
//Code Starts $.dough("cookieName", "cookieValue"); //Code Ends
Read Cookie
//Code Starts $.dough("cookieName"); //Code Ends
Delete Cookie
//Code Starts $.dough("cookieName", "remove"); //Code Ends
Available Options
This plugin also comes with options to play with.
- expires: Days ’til cookie expires
- path: Default is root ‘/’, set to ‘current’ to use the path of current page
- domain: Auto detect and set domain with subdomain prefix
- secure: Set to true if you’re using https://
Set full cookie
//Code Starts $.dough("cookieName", "cookieValue", { expires: 365, path: "current", domain: "auto", secure: true }); //Code Ends
Example cookie has a name of "cookieName", a value of "cookieValue", will expire in 1 year, have path of current page, domain will be autodetected and is set to secure for a use under https://. The beauty of this plugin it also supports JSON cookies. So using this plugin, you can create JSON cookies.
Create JSON cookie
//Code Starts $.dough("jsonCookie", "{'someName':'someValue','someOtherName':'someOtherValue'}"); //Code Ends
Read JSON cookie
//Code Starts $.dough("jsonCookie").someName; //Code Ends
This plugin is created by Nathan Searles.
Feel free to contact me for any help related to jQuery, I will gladly help you.