1. function setCookie(name, value, expires, path, domain, secure) {
    2. var cookieText = encodeURIComponent(name) + '=' + encodeURIComponent(value);
    3. if (expires instanceof Date) {
    4. cookieText += '; expires=' + expires;
    5. }
    6. if (path) {
    7. cookieText += "; path=" + path }
    8. if (domain) {
    9. cookieText += '; domain=' + domain;
    10. }
    11. if (secure) {
    12. cookieText += '; secure';
    13. }
    14. document.cookie = cookieText;
    15. }
    16. // 获取cookie
    17. function getCookie(name) {
    18. var cookieName = encodeURIComponent(name) + '=';
    19. var cookieStart = document.cookie.indexOf(cookieName);
    20. var cookieValue = null;
    21. if (cookieStart > -1) {
    22. var cookieEnd = document.cookie.indexOf(';', cookieStart);
    23. if (cookieEnd == -1) {
    24. cookieEnd = document.cookie.length;
    25. }
    26. cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length, cookieEnd));
    27. }
    28. return cookieValue;
    29. }
    30. // 删除cookie
    31. function unsetCookie(name) {
    32. document.cookie = name + "= ; expires=" + new Date(0);
    33. }