Set, Get, Check and Null Cookies with Jquery


/ Published in: jQuery
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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);

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.