Posted By

vagrantradio on 10/21/10


Tagged

cookies


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

mloberg
dekehooper
Tyster
laurentzziu
czehner
vladymy


Set, Get, Check and Null Cookies with Jquery


 / Published in: jQuery
 

URL: http://www.vagrantradio.com/2009/10/getting-and-setting-cookies-with-jquery.html

How to set, get, check for and remove cookies with jQuery.

  1. //set cookies
  2. $("#div").toggle(function() { //create div toggle
  3. $("#div").slideUp("slow"); //create effect
  4. $.cookie('cookie_name', 'value', {expires: 7}); //set cookie expire in 7 days
  5. },
  6. function () { //continue the toggle
  7. $("#div").slideDown("slow"); //create effect
  8. $.cookie('cookie_name', 'value'); //set cookie
  9. });
  10.  
  11. //get cookie
  12. alert( $.cookie("cookie_name") );
  13.  
  14. //check for cookies
  15. var cookie_name = $.cookie('cookie_name'); //set cookie var
  16. if (cookie_name == 'value') { //if equals this val
  17. $("#div").hide(); //do this
  18. };
  19.  
  20. //check for null cookies
  21. var cookie_name = $.cookie('cookie_name'); //set cookie var
  22. if (cookie_name == null) { // if empty
  23. $("#div").hide(); //do this
  24. };
  25.  
  26. //null cookies, same as getting
  27. $.cookie("cookie_name", null);

Report this snippet  

You need to login to post a comment.