jQuery Cookies (Yummy…)

I found many people get too comfortable with server-side script that even for setting up cookies, they use server-side script. True, some visitors may turn off javascript. But, if your application required javascript, why not setting up cookies with client-side script? It gives you better performance anyway.

Of course, jQuery is the way to go here. The plugin can be downloaded here (The author is carhartl)

http://plugins.jquery.com/project/Cookie

After download, you will need to reference to it:

<script type="text/javascript" src="js/jquery/jquery.js"></script>
<script type="text/javascript" src="js/jquery/plugins/jquery.cookies.js"></script>

Set A Cookie

Set cookie named “YummyCookie” that contains “oreo, nabisco, chocolate chips, butter” value.

$.cookie("YummyCookie", "oreo, nabisco, chocolate chips, butter");

Set cookie named “YummyCookie” that contains “oreo, nabisco, chocolate chips, butter” value in which the cookie expires in 5 days and available for the current path level.

$.cookie("YummyCookie", "oreo, nabisco, chocolate chips, butter", { expires: 5 });

Set cookie named “YummyCookie” that contains “oreo, nabisco, chocolate chips, butter” value and available for all paths within your domain.

$.cookie("YummyCookie", "oreo, nabisco, chocolate chips, butter", { path: '/' });

Set cookie named “YummyCookie” that contains “oreo, nabisco, chocolate chips, butter” value in which the cookie expires in 3 days and available for ‘/kitchen’ path level.

$.cookie("YummyCookie", "oreo, nabisco, chocolate chips, butter", { expires: 3, path: '/kitchen' });

Get A Cookie

alert($.cookie("YummyCookie"));
var _myCookie = $.cookie("YummyCookie");

Delete A Cookie

$.cookie("YummyCookie", null);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s