cookie.js 914 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /**
  2. * 获取cookie
  3. * @param name
  4. * @returns {string|null}
  5. */
  6. function getCookie(name) {
  7. var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
  8. if (arr = document.cookie.match(reg))
  9. return (arr[2]);
  10. else
  11. return null;
  12. }
  13. /**
  14. * 设置cookie
  15. * @param c_name
  16. * @param value
  17. * @param expiredays
  18. */
  19. function setCookie (c_name, value, expiredays) {
  20. var exdate = new Date();
  21. exdate.setDate(exdate.getDate() + expiredays);
  22. document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
  23. };
  24. /**
  25. * 删除cookie
  26. * @param name
  27. */
  28. function delCookie (name) {
  29. var exp = new Date();
  30. exp.setTime(exp.getTime() - 1);
  31. var cval = getCookie(name);
  32. if (cval != null)
  33. document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
  34. };
  35. export default {
  36. getCookie : getCookie,
  37. setCookie : setCookie,
  38. delCookie : delCookie
  39. }