js/jquery使用cookie判断页面是否初次打开
参考文案:http://muyu89.blog.163.com/blog/static/1745871482013910111210158/
参考文案:http://www.feelcss.com/jquerycookie-judge-whether-the-user-first-visit.html
javascript:
cookie.js 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; } }
index.html code:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <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> <body> </body> </html>
jquery:
jquery.cookie.js code:
jQuery.cookie = function(name, value, options) { if (typeof value != 'undefined') { // name and value given, set cookie options = options || {}; if (value === null) { value = ''; options.expires = - 1; } var expires = ''; if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) { var date; if (typeof options.expires == 'number') { date = new Date(); date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000)); } else { date = options.expires; } expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE } // CAUTION: Needed to parenthesize options.path and options.domain // in the following expressions, otherwise they evaluate to undefined // in the packed version for some reason... var path = options.path ? '; path=' + (options.path) : ''; var domain = options.domain ? '; domain=' + (options.domain) : ''; var secure = options.secure ? '; secure': ''; document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join(''); } else { // only name given, get cookie var cookieValue = null; if (document.cookie && document.cookie != '') { var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = jQuery.trim(cookies[i]); // Does this cookie string begin with the name we want? if (cookie.substring(0, name.length + 1) == (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; } };
index.html code:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <script type="text/javascript" src="jquery-1.7.1.min.js"></script> <script type="text/javascript" src="jquery.cookie.js"></script> <body style="width: 100%; height: 200px;"> </body> <script type="text/javascript"> var options = { expires : 1 }; if ($.cookie('visits') == null) { //第一次访问 $.cookie('visits', '1', options); $("body").text("第一次访问"); }else{ //不是第一次访问 $("body").text("不是第一次访问"); } </script> </html>

浙公网安备 33010602011771号