jquery.cookie.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. //$.cookie('cookieName',null,{path: '/', expires: -1}); ;//清空cookie
  2. //$.cookie("cookieName", id, { path: '/', expires: 2 }); //加上cookie
  3. //接收 cookie var getCookie=$.cookie("cookieName");
  4. jQuery.cookie = function (name, value, options) {
  5. /// <summary>设置cookie</summary>
  6. if (typeof value != 'undefined') { // name and value given, set cookie
  7. options = options || {};
  8. if (value === null) {
  9. value = '';
  10. options.expires = -1;
  11. }
  12. var expires = '';
  13. if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
  14. var date;
  15. if (typeof options.expires == 'number') {
  16. date = new Date();
  17. date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
  18. } else {
  19. date = options.expires;
  20. }
  21. expires = '; expires=' + date.toUTCString();
  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. return cookieValue;
  40. }
  41. }