foundation.cookie.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*!
  2. * jQuery Cookie Plugin v1.3
  3. * https://github.com/carhartl/jquery-cookie
  4. *
  5. * Copyright 2011, Klaus Hartl
  6. * Dual licensed under the MIT or GPL Version 2 licenses.
  7. * http://www.opensource.org/licenses/mit-license.php
  8. * http://www.opensource.org/licenses/GPL-2.0
  9. *
  10. * Modified to work with Zepto.js by ZURB
  11. */
  12. (function ($, document, undefined) {
  13. var pluses = /\+/g;
  14. function raw(s) {
  15. return s;
  16. }
  17. function decoded(s) {
  18. return decodeURIComponent(s.replace(pluses, ' '));
  19. }
  20. var config = $.cookie = function (key, value, options) {
  21. // write
  22. if (value !== undefined) {
  23. options = $.extend({}, config.defaults, options);
  24. if (value === null) {
  25. options.expires = -1;
  26. }
  27. if (typeof options.expires === 'number') {
  28. var days = options.expires, t = options.expires = new Date();
  29. t.setDate(t.getDate() + days);
  30. }
  31. value = config.json ? JSON.stringify(value) : String(value);
  32. return (document.cookie = [
  33. encodeURIComponent(key), '=', config.raw ? value : encodeURIComponent(value),
  34. options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
  35. options.path ? '; path=' + options.path : '',
  36. options.domain ? '; domain=' + options.domain : '',
  37. options.secure ? '; secure' : ''
  38. ].join(''));
  39. }
  40. // read
  41. var decode = config.raw ? raw : decoded;
  42. var cookies = document.cookie.split('; ');
  43. for (var i = 0, l = cookies.length; i < l; i++) {
  44. var parts = cookies[i].split('=');
  45. if (decode(parts.shift()) === key) {
  46. var cookie = decode(parts.join('='));
  47. return config.json ? JSON.parse(cookie) : cookie;
  48. }
  49. }
  50. return null;
  51. };
  52. config.defaults = {};
  53. $.removeCookie = function (key, options) {
  54. if ($.cookie(key) !== null) {
  55. $.cookie(key, null, options);
  56. return true;
  57. }
  58. return false;
  59. };
  60. })(Foundation.zj, document);