js操作cookie方法

Posted by
//js获取cookie
/*获取Cookie值*/
function getCookie(c_name)
{
if(document.cookie.length>0){
   c_start=document.cookie.indexOf(c_name + “=”)
   if(c_start!=-1){
     c_start=c_start + c_name.length+1
     c_end=document.cookie.indexOf(“;”,c_start)
     if(c_end==-1) c_end=document.cookie.length
     return unescape(document.cookie.substring(c_start,c_end))
   }
}
return “”
}
**********************************//JS操作cookies方法! *****************************************************************
//写cookies
function setCookie(name,value)
{
 var Days = 30;
 var exp = new Date();
 exp.setTime(exp.getTime() + Days*24*60*60*1000);
 document.cookie = name + “=”+ escape (value) + “;expires=” + exp.toGMTString();
}
//读取cookies
function getCookie(name)
{
 var arr,reg=new RegExp(“(^| )”+name+”=([^;]*)(;|$)”);
 if(arr=document.cookie.match(reg))
  return unescape(arr[2]);
 else
  return null;
}
//删除cookies
function delCookie(name)
{
 var exp = new Date();
 exp.setTime(exp.getTime() – 1);
 var cval=getCookie(name);
 if(cval!=null)
  document.cookie= name + “=”+cval+”;expires=”+exp.toGMTString();
}
//使用示例
setCookie(“name”,”hayden”);
alert(getCookie(“name”));
//如果需要设定自定义过期时间
//那么把上面的setCookie 函数换成下面两个函数就ok;
//程序代码
function setCookie(name,value,time)
{
 var strsec = getsec(time);
 var exp = new Date();
 exp.setTime(exp.getTime() + strsec*1);
 document.cookie = name + “=”+ escape (value) + “;expires=” + exp.toGMTString();
}
function getsec(str)
{
   alert(str);
   var str1=str.substring(1,str.length)*1;
   var str2=str.substring(0,1);
   if (str2==”s”)
   {
     return str1*1000;
   }
   else if (str2==”h”)
   {
    return str1*60*60*1000;
   }
   else if (str2==”d”)
   {
    return str1*24*60*60*1000;
   }
}
//这是有设定过期时间的使用示例:
//s20是代表20秒
//h是指小时,如12小时则是:h12
//d是天数,30天则:d30
setCookie(“name”,”aaron”,”s20″);

30 comments

  1. I simply want to tell you that I’m very new to blogs and truly liked this page. Likely I’m likely to bookmark your blog . You amazingly come with outstanding writings. With thanks for sharing your web-site.

  2. I simply want to mention I’m beginner to blogging and certainly loved your page. Likely I’m going to bookmark your blog . You certainly have very good well written articles. Bless you for sharing with us your web page.

  3. I simply want to mention I am new to blogging and seriously enjoyed this web blog. Likely I’m likely to bookmark your website . You really have awesome articles. Appreciate it for revealing your blog site.

  4. Hi! I realize this is kind of off-topic however I needed to ask.
    Does running a well-established blog like yours require a lot of work?
    I’m completely new to running a blog but I do write in my journal on a
    daily basis. I’d like to start a blog so I will be able to share my personal experience and feelings
    online. Please let me know if you have any recommendations
    or tips for new aspiring blog owners. Thankyou!

  5. Although web sites we backlink to below are considerably not connected to ours, we really feel they’re in fact really worth a go as a result of, so possess a look.

  6. Hi there very cool blog!! Guy .. Excellent .. Superb ..
    I will bookmark your site and take the feeds additionally?
    I am satisfied to find numerous helpful information right here in the post, we need develop more
    techniques on this regard, thank you for sharing. . . . . .

  7. I simply want to mention I am just all new to blogging and site-building and really liked this website. Likely I’m planning to bookmark your website . You definitely come with amazing articles. Bless you for sharing your web site.

  8. I’ve read this post and if I could I want to suggest you few interesting things or suggestions. Maybe you can write next articles referring to this article. I want to read more things about it!|

  9. Nice blog here! Additionally your site lots up very fast! What web host are you the use of? Can I get your associate hyperlink on your host? I want my web site loaded up as fast as yours lol

  10. Excellent blog! Do you have any tips for aspiring writers? I’m planning to start my own blog soon but I’m a little lost on everything. Would you recommend starting with a free platform like WordPress or go for a paid option? There are so many options out there that I’m totally confused .. Any suggestions? Many thanks!|

  11. Hello there! Quick question that’s entirely off topic. Do you know how to make your site mobile friendly? My website looks weird when viewing from my iphone 4. I’m trying to find a template or plugin that might be able to fix this problem. If you have any recommendations, please share. Cheers!|

Comments are closed.