/*** *讀取指定的Cookie值 readCookie("id"); *@param {string} cookieName Cookie名稱(chēng) */ function readCookie(cookieName) { var theCookie = "" + document.cookie; var ind = theCookie.indexOf(cookieName); if(ind==-1 || cookieName=="") return ""; var ind1 = theCookie.indexOf(';',ind); if(ind1==-1) ind1 = theCookie.length; /*讀取Cookie值*/ return unescape(theCookie.substring(ind+cookieName.length+1,ind1)); } /*** * 設(shè)置Cookie值 setCookie("id",1); * @param {string} cookieName Cookie名稱(chēng) * @param {string} cookieValue Cookie值 * @param {number} nDays Cookie過(guò)期天數(shù) */ function setCookie(cookieName, cookieValue) { /*當(dāng)前日期*/ var today = new Date(); /*Cookie過(guò)期時(shí)間*/ var expire = new Date(); /*如果未設(shè)置nDays參數(shù)或者nDays為0,取默認(rèn)值1*/ //if(nDays == null || nDays == 0) nDays = 1; /*計(jì)算Cookie過(guò)期時(shí)間【 3600000 * 24 為一天】*/ expire.setTime(today.getTime() + 400000); //5分鐘 document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString(); } /*** * 刪除cookie中指定變量函數(shù) * @param {string} $name Cookie名稱(chēng) */ function deleteCookie($name){ var myDate=new Date(); myDate.setTime(-1000);//設(shè)置時(shí)間 document.cookie=$name+"=''; expires="+myDate.toGMTString(); } /*** * 刪除cookie中所有定變量函數(shù) * @param {string} cookieName Cookie名稱(chēng) * @param {string} cookieValue Cookie值 * @param {number} nDays Cookie過(guò)期天數(shù) */ function clearCookie(){ var myDate=new Date(); myDate.setTime(-1000);//設(shè)置時(shí)間 var data=document.cookie; var dataArray=data.split("; "); for(var i=0;i<dataArray.length;i++){ var varName=dataArray[i].split("="); document.cookie=varName[0]+"=''; expires="+myDate.toGMTString(); } }
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com