JS获取cookie值倒是可以

本文提供了一组JavaScript函数,用于设置、获取和删除浏览器的Cookie。这些函数可以帮助开发者轻松地管理网站用户的会话信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

  1. //设定cookie值   
  2. function   setCookie(name,   value,   domain)   {   
  3. document.cookie   =   "[ "   +   name   +   "]= "   +   escape(value)   + ";path=/ "+   ((domain)? ";domain= "+domain: " ")   //   +   ((expire   ==   null)   ?   " "   :   ( ";   expires= "   +   expire.toGMTString()))   + ";secure "   
  4. }//:$   
  5. //读取cookie值   
  6. function   getCookie(Name)   {   
  7. var   search   =   "[ "   +   Name   +   "]= "   
  8. if   (document.cookie.length   >   0)   {   //   if   there   are   any   cookies   
  9. offset   =   document.cookie.indexOf(search)   
  10. if   (offset   !=   -1)   {   //   if   cookie   exists   
  11. offset   +=   search.length   
  12. //   set   index   of   beginning   of   value   
  13. end   =   document.cookie.indexOf( "; ",   offset)   
  14. //   set   index   of   end   of   cookie   value   
  15. if   (end   ==   -1)   
  16. end   =   document.cookie.length   
  17. return   unescape(document.cookie.substring(offset,   end))   
  18. }   
  19. }   
  20. }//:$   
  21. //删除cookie值   
  22. function   deleteCookie(name,path,domain){   
  23. if(getCookie(name))   document.cookie   =   "[ "+   name   + "]= "+   ((path)? ";path= "+path: " ")   +   ((domain)? ";domain= "+domain: " ")   //+ ";expires=Thu,01-Jan-70   00:00:01   GMT "   
  24. }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值