How to use cookies in jQuery

Last few months I have kind of drifted away from Flash (don’t worry I also have some Flash projects in my mind). Currently I am working on few WordPress themes so I needed to learn a lot of new stuff. I started my journey with web development from Flash so I didn’t have strong HTML/CSS background, I had to learn it now… I spend last few weeks on polishing my knowledge of HTML, CSS, PHP, jQuery and of course WordPress. I have to say it is a completely different pair of shoes but I like it :) Ok, let’s get to the point of this post.


Using cookies in jQuery is extremely easy but it may take some time to figure it out if you don’t know where to start.

First of all you need to know that cookies are not supported by core jQuery library, to easily manage cookies you will need to download this plugin: http://plugins.jquery.com/project/Cookie (I am not the author).

Setting Cookies

Once you embed the script into your code (include it like any other script) you can set cookies like this:

$.cookie("attribute", "value");

This is all you need to do, this cookie will be deleted after the session ends (which means that it will be deleted when you close your browser). If you want to, you can set the number of days that cookie will last, for example:

$.cookie("attribute", "value", { expires: 1 });

Cookie from above example will expire after one day. You can specify the path at which the cookie will be available. For example this cookie will be available on all paths at your domain:

$.cookie("attribute", "value", { path: '/' });

and this only at the ‘blog’ subpath:

$.cookie("attribute", "value", { path: 'blog' });

Getting Cookies

To get the cookie you have to know its name, for example:

var cookieValue = $.cookie("attribute");

Deleting Cookies

At any time you can delete your cookie, to that just set it’s value to ‘null’.

$.cookie("attribute", null);


As you can see setting and getting cookies in jQuery is extremely easy. Tell me what you think about this quick tip (and tell me if you want to see more posts like this). Good to be back. I will try to blog more often!

Related Posts

About the Author: Kuba Gaj

Hi, my name is Kuba and I am the founder of massiveProCreation. I am interested in almost everything that is connected to technology (specially Adobe Flash). If you have any questions or suggestions please feel free to contact me :)

6 Comments + Add Comment

  • Could you please post an example in VB. I think that would make it easier to understand. Thank you.

  • Hi, What do you mean by VB? If you mean VisualBasic this is article about jQuery sorry ;)

  • Managing state in web-dev was always kinda tricky. You said, that you just began your journey in web-dev – my knowledge in web-dev is probably still much beneath your level – but want to share some thought with You. Managing state is really a tricky thing in web-dev. You know – you have many tcp connection over a http, and each connection is kinda unique for your server-side code. So state is not only a point in application, where user stands, but also a way to determine which specific user is doing in your app. The very same thing happens in client-side – you probably want to save some state, in-between your scripts. Cookies are just one of possibilities. In ASP.NET, we have a server-side Sessions,ApplicationState, Cache objects with invalidation, on client-side there are cookies, hidden-fields, query strings.. Every solution have its pros and cons, and its really a challenge to find state-saving solution suitable for your app and requirements.. such things like performance, serialization costs, security.. its really bigger topic than it could appear at first.. Kuba, I havent been it touch with you from some time – what server-side technologies are you using. I know that you were in-deep with Flash and Adobe technologies. Maybe some posts about sharing state between Flash and JQuery, or Flash and some server-side technologies. cheers.

  • Hi man, I don’t have any experience in ASP.NET but I have some with Flash and server side technologies, the thing is all the info to share something between Flash and server (via PHP) can be found on gotoandlearn.com There is an awesome tutorial series by Lee Brimelow about AMFPHP ;)

  • :)

Leave a comment

Become an Author

We are looking for Authors, if you have a Flash (or related) knowledge and you want to share it with the community on our blog please contact us.