jquery.cookie.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. jQuery.cookie = function (name, value, options) {
  2. if (typeof value != 'undefined') {
  3. options = options || {};
  4. if (value === null) {
  5. value = '';
  6. options = $.extend({}, options);
  7. options.expires = -1
  8. }
  9. ;
  10. var expires = '';
  11. if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
  12. var date;
  13. if (typeof options.expires == 'number') {
  14. date = new Date();
  15. date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000))
  16. } else {
  17. date = options.expires
  18. }
  19. ;
  20. expires = '; expires=' + date.toUTCString()
  21. }
  22. ;
  23. var path = options.path ? '; path=' + (options.path) : '';
  24. var domain = options.domain ? '; domain=' + (options.domain) : '';
  25. var secure = options.secure ? '; secure' : '';
  26. document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('')
  27. } else {
  28. var cookieValue = null;
  29. if (document.cookie && document.cookie != '') {
  30. var cookies = document.cookie.split(';');
  31. for (var i = 0; i < cookies.length; i++) {
  32. var cookie = jQuery.trim(cookies[i]);
  33. if (cookie.substring(0, name.length + 1) == (name + '=')) {
  34. cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
  35. break
  36. }
  37. }
  38. }
  39. ;
  40. return cookieValue
  41. }
  42. };