免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2447 | 回复: 1
打印 上一主题 下一主题

jquery 写cookie [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-29 22:33 |只看该作者 |倒序浏览

jquery 写cookie









Js代码
  1. 1.jQuery.cookie = function(name, value, options) {  
  2. 2.        if (typeof value != 'undefined') { // name and value given, set cookie  
  3. 3.            options = options || {};  
  4. 4.            if (value === null) {  
  5. 5.                value = '';  
  6. 6.                options.expires = -1;  
  7. 7.            }  
  8. 8.            var expires = '';  
  9. 9.            if (options.expires  
  10. 10.                    && (typeof options.expires == 'number' || options.expires.toUTCString)) {  
  11. 11.                var date;  
  12. 12.                if (typeof options.expires == 'number') {  
  13. 13.                    date = new Date();  
  14. 14.                    date.setTime(date.getTime()  
  15. 15.                            + (options.expires * 24 * 60 * 60 * 1000));  
  16. 16.                } else {  
  17. 17.                    date = options.expires;  
  18. 18.                }  
  19. 19.                expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE  
  20. 20.            }  
  21. 21.            var path = options.path ? '; path=' + options.path : '';  
  22. 22.            var domain = options.domain ? '; domain=' + options.domain : '';  
  23. 23.            var secure = options.secure ? '; secure' : '';  
  24. 24.            document.cookie = [ name, '=', encodeURIComponent(value), expires,  
  25. 25.                    path, domain, secure ].join('');  
  26. 26.        } else { // only name given, get cookie  
  27. 27.            var cookieValue = null;  
  28. 28.            if (document.cookie && document.cookie != '') {  
  29. 29.                var cookies = document.cookie.split(';');  
  30. 30.                for ( var i = 0; i < cookies.length; i++) {  
  31. 31.                    var cookie = jQuery.trim(cookies[i]);  
  32. 32.                    if (cookie.substring(0, name.length + 1) == (name + '=')) {  
  33. 33.                        cookieValue = decodeURIComponent(cookie  
  34. 34.                                .substring(name.length + 1));  
  35. 35.                        break;  
  36. 36.                    }  
  37. 37.                }  
  38. 38.            }  
  39. 39.            return cookieValue;  
  40. 40.        }  
  41. 41.    };  
复制代码
页面引用:



Js代码
  1. 1.$.cookie( name, value, options );  
复制代码
参数解释:

--name 写入cookie的key


--value 写入cookie的value


-- options 存放的时限

论坛徽章:
0
2 [报告]
发表于 2011-12-29 22:33 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP