jQuery Method How to - Get/set value from cookie with cookie()








Question

We would like to know how to get/set value from cookie with cookie().

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.10.1.js'></script>
<script type='text/javascript'
  src="http://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.3.1/jquery.cookie.min.js"></script>
<script type='text/javascript'>
$(function(){<!--   w  w w  .j  av  a 2  s . co  m-->
if ($.cookie('test_status') != '1')
{
    setTimeout(function()
    {
        console.log('foo');
        jQuery.cookie('test_status', '1', { expires: 31}); 
    }, 1000);
}
});
</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: