JS通過Cookie判斷頁面是否為首次打開
更新時間:2016年02月05日 12:04:19 作者:曉囡
這篇文章主要介紹了JS通過Cookie判斷頁面是否為首次打開 的相關(guān)資料,需要的朋友可以參考下
廢話不多說了,直接給大家貼代碼了,本文寫的不好還請各位大俠見諒。
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參數(shù)錯誤");
}
}
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內(nèi)容為空
alert("首次打開頁面");
//設(shè)置保存時間
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"); //設(shè)置Cookie。只要IE不關(guān)閉,Cookie就一直存在
}
else{//Cookie存在,表示頁面是被刷新的
alert("頁面刷新");
}
}
</script>
以上所述是小編給大家分享JS通過Cookie判斷頁面是否為首次打開的相關(guān)內(nèi)容,希望對大家有所幫助。
相關(guān)文章
基于html+css+js實現(xiàn)簡易計算器代碼實例
這篇文章主要介紹了基于html+css+js實現(xiàn)簡易計算器代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-02-02
ES6(ECMAScript 6)新特性之模板字符串用法分析
這篇文章主要介紹了ES6(ECMAScript 6)新特性之模板字符串用法,簡單介紹了ES6模板字符串的概念、功能并結(jié)合實例形式分析了ES6模板字符串的用法,需要的朋友可以參考下2017-04-04
博客側(cè)邊欄模塊跟隨滾動條滑動固定效果的實現(xiàn)方法(js+jquery等)
現(xiàn)在很多的獨立博客和網(wǎng)站如人人網(wǎng)等,都使用了讓側(cè)邊欄模塊隨滾動條滑動而位置固定的效果2013-03-03
JS 使用for循環(huán)遍歷子節(jié)點查找元素
這篇文章主要介紹了JS 使用for循環(huán)配合數(shù)組遍歷子節(jié)點查找元素,經(jīng)測試,效果不錯,需要的朋友可以看看2014-09-09

