kongquexue的gravatar头像
kongquexue 2013-10-10 10:16:38

js Cookie 判断页面是否为第一次打开

JScript code:
function Cookie(key,value)
{
    this.key=key;
    if(value!=null)
    {
        this.value=escape(value);
    }
    this.expiresTime=null;
    this.domain=null;
    this.path="/";
    this.secure=null;
}
Cookie.prototype.setValue=function(value){this.value=escape(value);}
Cookie.prototype.getValue=function(){return (this.value);}

Cookie.prototype.setExpiresTime=function(time){this.expiresTime=time;}
Cookie.prototype.getExpiresTime=function(){return this.expiresTime;}

Cookie.prototype.setDomain=function(domain){this.domain=domain;}
Cookie.prototype.getDomain=function(){return this.domain;}

Cookie.prototype.setPath=function(path){this.path=path;}
Cookie.prototype.getPath=function(){return this.path;}

Cookie.prototype.Write=function(v)
{
    if(v!=null)
    {
        this.setValue(v);
    }
    var ck=this.key+"="+this.value;
    if(this.expiresTime!=null)
    {
        try
        {
            ck+=";expires="+this.expiresTime.toUTCString();;
        }
        catch(err)
        {
            alert("expiresTime参数错误");
        }
    }
    if(this.domain!=null)
    {
        ck+=";domain="+this.domain;
    }
    if(this.path!=null)
    {
        ck+=";path="+this.path;
    }
    if(this.secure!=null)
    {
        ck+=";secure";
    }
    document.cookie=ck;
}
Cookie.prototype.Read=function()
{
    try
    {
        var cks=document.cookie.split("; ");
        var i=0;
        for(i=0;i <cks.length;i++)
        {
            var ck=cks[i];
            var fields=ck.split("=");
            if(fields[0]==this.key)
            {
                this.value=fields[1];
                return (this.value);
            }
        }
        return null;
    }
    catch(err)
    {
        alert("cookie读取错误");
        return null;
    }
}

HTML code:
<script type="text/javascript" src="Cookie.js"></script>
<script type="text/javascript" language="javascript">
 window.onload=function(){
  var ck=new Cookie("HasLoaded");  //每个页面的new Cookie名HasLoaded不能相同
  if(ck.Read()==null){//未加载过,Cookie内容为空
      alert("首次打开页面");
    
     //设置保存时间
     var dd = new Date();
     dd = new Date(dd.getYear() + 1900, dd.getMonth(), dd.getDate());
     dd.setDate(dd.getDate() + 365);
     ck.setExpiresTime(dd);

      ck.Write("true");  //设置Cookie。只要IE不关闭,Cookie就一直存在
  }
  else{//Cookie存在,表示页面是被刷新的
   alert("页面刷新");
  }
 }
</script>

打赏

文件名:js.zip,文件大小:1K 下载
  • /
    • /js.txt
最代码最近下载分享源代码列表最近下载
lxh2845  LV2 2018年9月6日
sb9876  LV1 2017年10月24日
zeming  LV1 2016年4月27日
邀仙赏月  LV16 2015年6月7日
风铃  LV32 2015年6月1日
wzftop  LV1 2014年4月24日
holysir  LV28 2014年2月15日
while  LV1 2014年2月12日
hooyo  LV1 2013年12月11日
ypc  LV24 2013年10月21日
最代码最近浏览分享源代码列表最近浏览
cab123123  LV8 2022年4月24日
no one and you  LV2 2020年6月8日
wsx456521  LV10 2020年5月4日
yangjiayangjia  LV3 2020年4月24日
是你的锐哥哦  LV2 2020年1月16日
280286951  LV17 2019年9月7日
xuechengchang  LV4 2019年6月10日
zenghang188  LV12 2019年3月13日
luomazkf  LV15 2019年3月5日
艾登艾登艾登  LV8 2019年1月11日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友