LsyCookie.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* *
  2. * LsyCookie
  3. * author loushengyue
  4. * website http://www.loushengyue.com
  5. * version 1.2.4
  6. * methods [setItem(),getItem(),getAll(),removeItem(),,clear()]
  7. */
  8. ;(function (win, doc) {
  9. var LsyCookie, expiresTime = 24 * 3600, version = '1.2.4';
  10. LsyCookie = function () {
  11. this.version = 'LsyCookie ' + version;
  12. };
  13. /* *
  14. * create cookie by key and value, and set expires time
  15. * key typeof string
  16. * val typeof string
  17. * path typeof string
  18. * time typeof number[7days]
  19. */
  20. LsyCookie.prototype.setItem = function (key, val, time, path) {
  21. var exp, pathStr = '';
  22. checkStr(key);
  23. val = resetVal(val);
  24. time = resetTime(time);
  25. exp = new Date();
  26. exp.setTime(exp.getTime() + time * 1000);
  27. if (path) {
  28. checkStr(path);
  29. path = resetPath(path);
  30. pathStr = path ? ';path=' + path : pathStr;
  31. }
  32. doc.cookie = key + '=' + val + ';expires=' + exp.toGMTString() + pathStr;
  33. };
  34. /* *
  35. * remove cookie by key
  36. * key typeof string
  37. */
  38. LsyCookie.prototype.removeItem = function (key, path) {
  39. path = path ? path : './';
  40. this.set(key, '', -1, path);
  41. };
  42. /* *
  43. * remove all cookies
  44. */
  45. LsyCookie.prototype.clear = function () {
  46. var _this = this;
  47. var keys = Object.keys(_this.getAll());
  48. keys.forEach(function (item) {
  49. _this.removeItem(item);
  50. });
  51. };
  52. /* *
  53. * get all cookies, and return object
  54. */
  55. LsyCookie.prototype.getAll = function () {
  56. var cookies = doc.cookie.split(/;\s/);
  57. var cookieObj = {};
  58. cookies.forEach(function (item) {
  59. var key = item.split('=')[0];
  60. var val = item.split('=')[1];
  61. val = resetJsonStr(val);
  62. cookieObj[key] = val;
  63. });
  64. return cookieObj;
  65. };
  66. /* *
  67. * get cookie by key
  68. * key typeof string
  69. */
  70. LsyCookie.prototype.getItem = function (key) {
  71. return this.getAll()[key];
  72. };
  73. /* *
  74. * reset expires time
  75. * time typeof number[7days]
  76. */
  77. function resetTime(time) {
  78. if (time != null) {
  79. if (isNaN(time)) {
  80. throw new Error('The typeof time argument in resetTime(time){...} must be number. But the typeof your argument "' + time + '" is ' + typeof time);
  81. }
  82. time = time > 0 ? time : -1;
  83. } else {
  84. time = expiresTime;
  85. }
  86. return time;
  87. }
  88. /* *
  89. * ckeck val is the right typeof string, if not, change it.
  90. * val typeof String,Array,Object
  91. * return typeof string
  92. */
  93. function resetVal(val) {
  94. if (typeof val === 'object') {
  95. val = JSON.stringify(val);
  96. }
  97. return val;
  98. }
  99. /* *
  100. * ckeck key is the right typeof string, if not, change it.
  101. * val typeof String,Array,Object
  102. * return typeof string
  103. */
  104. function checkStr(key) {
  105. if (typeof key != 'string') {
  106. throw new Error('The typeof str argument in setItem(key[string],value[string|object]){...} must be string. But the typeof your argument "' + key + '" is ' + typeof key);
  107. }
  108. }
  109. /* *
  110. * reset string for json.
  111. * return typeof String,Array,Object
  112. */
  113. function resetJsonStr(str) {
  114. var obj;
  115. try {
  116. obj = JSON.parse(str);
  117. return obj;
  118. } catch (e) {
  119. return str;
  120. }
  121. }
  122. /* *
  123. * reset path
  124. * path typeof string
  125. */
  126. function resetPath(path) {
  127. if (/^(\.)?\.\//.test(path)) {
  128. var timesArr, n,
  129. index = -1,
  130. pathName = location.pathname;
  131. path = path.replace(/^\.\//, '');
  132. timesArr = path.match(/\.\.\//g) || [];
  133. n = timesArr.length + 1;
  134. for (; n--;) {
  135. index = pathName.lastIndexOf('/');
  136. if (index != -1) {
  137. pathName = pathName.substr(0, index);
  138. }
  139. }
  140. path = path.replace(/\.\.\//g, '');
  141. return pathName + '/' + path;
  142. }
  143. return path;
  144. }
  145. win['LsyCookie'] = new LsyCookie();
  146. })(window, document);