JS通過Cookie判斷頁面是否為首次打開
廢話不多說了,直接給大家貼代碼了,本文寫的不好還請(qǐng)各位大俠見諒。
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ù)錯(cuò)誤"); } } 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讀取錯(cuò)誤"); 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"); //每個(gè)頁面的new Cookie名HasLoaded不能相同 if(ck.Read()==null){//未加載過,Cookie內(nèi)容為空 alert("首次打開頁面"); //設(shè)置保存時(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)容,希望對(duì)大家有所幫助。
相關(guān)文章
使用JS獲取input file的路徑C:\fakepath\問題及解決方法
這篇文章主要介紹了使用JS獲取input file的路徑C:\fakepath\問題及解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01基于JavaScript實(shí)現(xiàn)圖片放大鏡功能
在一些電商網(wǎng)站上,經(jīng)??吹接猩唐穲D片被放大查看的功能,所以本文將使用前端技術(shù)實(shí)現(xiàn)一個(gè)簡單的圖片放大鏡功能,希望能給大家?guī)硪欢ǖ膸椭?/div> 2023-06-06select2 ajax 設(shè)置默認(rèn)值,初始值的方法
今天小編就為大家分享一篇select2 ajax 設(shè)置默認(rèn)值,初始值的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08通過V8源碼看一個(gè)關(guān)于JS數(shù)組排序的詭異問題
一直在學(xué)習(xí)C++,也想閱讀點(diǎn)開源的C++項(xiàng)目,發(fā)現(xiàn)網(wǎng)上對(duì)Google V8評(píng)價(jià)不錯(cuò),于是上Github上找到了源代碼,但在學(xué)習(xí)中遇到一個(gè)js數(shù)組排序的問題,下面這篇文章主要給大家介紹了通過V8源碼說說一個(gè)關(guān)于JS數(shù)組排序的詭異問題的相關(guān)資料,需要的朋友可以參考下。2017-08-08javascript 隱藏/顯示指定的區(qū)域附HTML元素【legend】用法
今日閑來無事就寫寫JS,用來顯示/隱藏制定的DIV區(qū)域。2010-03-03微信小程序 功能函數(shù)小結(jié)(手機(jī)號(hào)驗(yàn)證*、密碼驗(yàn)證*、獲取驗(yàn)證碼*)
這篇文章主要介紹了微信小程序功能函數(shù)手機(jī)號(hào)驗(yàn)證、密碼驗(yàn)證,獲取驗(yàn)證碼*的相關(guān)代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-12-12一次記住JavaScript的6個(gè)正則表達(dá)式方法
這篇文章主要介紹了一次記住JavaScript的6個(gè)正則表達(dá)式方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-02-02js動(dòng)態(tài)添加帶圓圈序號(hào)列表的實(shí)例代碼
這篇文章主要介紹了js動(dòng)態(tài)添加帶圓圈序號(hào)列表的實(shí)例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02最新評(píng)論