Setting and Getting Cookies in jQuery

Date Posted: September 2, 2014, 6:57 am


In this tutorial, we are going to use a jQuery library called jquery.cookie.min.js so that we can access cookies like this: $.cookie(‘name’)

Set a cookie:

1
2
3
4
5
6
7
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script type="text/javascript">
    jQuery(document).ready(function($){
        $.cookie('cname', 'cvalue', { path: '/', expires: 1 });
    });
</script>

Retrieve the cookie:

1
$.cookie("cname")

About author


Carl Victor Fontanos

A fine gentleman specializing in front-end and back-end development with extensive experience building high performance web applications that keeps users engaged and help businesses grow.

 
Hire Me