JavaScript操作Cookie方法實(shí)例分析
本文實(shí)例講述了JavaScript操作Cookie方法。分享給大家供大家參考。具體如下:
// My methods for setting, reading and deleting cookies. // I have methods to check for the existence of cookie names or values, // to retrieve by name or value, and to create a formatted string of // all the cookies. // My site: andrew.dx.am var SetCookie = function (name, value, expires, path, domain, secure) { // The caller should Trim the name/value pair, if required. // Sets the name/value pair (encoded); 'expires' is the no. of days. var expires_date; if (expires) { expires_date = new Date(); expires_date.setDate(expires_date.getDate() + expires); } document.cookie = encodeURIComponent(name) + "=" + encodeURIComponent(value) + ( ( expires ) ? ";expires=" + expires_date.toUTCString() : "" ) + ( ( path ) ? ";path=" + path : "" ) + ( ( domain ) ? ";domain=" + domain : "" ) + ( ( secure ) ? ";secure" : "" ); }; var DeleteCookie = function (name, path, domain) { // The caller should Trim the name/value pair. // Encodes the name before deleting. document.cookie = encodeURIComponent(name) + "=" + ( ( path ) ? ";path=" + path : "") + ( ( domain ) ? ";domain=" + domain : "" ) + ";expires=Fri, 01-Jan-2010 00:00:01 UTC"; }; var DelAllCookies = function () { var currDate = new Date(), i, theCookie = document.cookie.split(";"); currDate = currDate.toUTCString(); i = theCookie.length; while ( i-- ) { document.cookie = theCookie[i] + "; expires =" + currDate; } }; var EscapeReg = function (str) { // Helper fn: Escapes characters for use in a regular expression. return str.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); }; // The following four functions do not Trim the name or value // - the calling fns should do this. var CNameExists = function (cookie_name) { // case-insensitive var testName, myReg; if (document.cookie.length == 0) return false; testName = EscapeReg(cookie_name); myReg = new RegExp('(^|;) ?' + testName + '=([^;]*)(;|$)','i'); return myReg.test(decodeURIComponent(document.cookie)); }; var CValueExists = function (cookie_value) { // case insensitive var testName, myReg; if (document.cookie.length == 0) return false; testName = EscapeReg(cookie_value); myReg = new RegExp('(=)' + testName + '(;|$)','i'); return myReg.test(decodeURIComponent(document.cookie)); }; var CNameGet = function (cookie_value) { // case-insensitive var testName, myReg, results; if (document.cookie.length == 0) return ''; testName = EscapeReg(cookie_value); myReg = new RegExp('(^|;) ?([^=]*)=' + testName + '(;|$)','i'); results = decodeURIComponent(document.cookie).match(myReg); return ( results ) ? results[2] : ''; }; var CValueGet = function (cookie_name) { // case-insensitive var testName, myReg, results; if (document.cookie.length == 0) return ''; testName = EscapeReg(cookie_name); myReg = new RegExp('(^|;) ?' + testName + '=([^;]*)(;|$)','i'); results = decodeURIComponent(document.cookie).match(myReg); return ( results ) ? results[2] : ''; }; var CookieStr = function () { // Returns a string (with line breaks) which could be // placed in, for example, a textarea. return decodeURIComponent(document.cookie). replace(/([^=;]+)=([^;]*)[;\s]*/g,'$1 ($2)\n') || ''; };
希望本文所述對(duì)大家的javascript程序設(shè)計(jì)有所幫助。
- JavaScript實(shí)現(xiàn)cookie的寫(xiě)入、讀取、刪除功能
- javascript封裝 Cookie 應(yīng)用接口
- JavaScript對(duì)Cookie進(jìn)行讀寫(xiě)操作實(shí)例
- JavaScript中Cookies的相關(guān)使用教程
- javascript實(shí)現(xiàn)設(shè)置、獲取和刪除Cookie的方法
- JavaScript使用cookie實(shí)現(xiàn)記住賬號(hào)密碼功能
- JavaScript實(shí)現(xiàn)基于Cookie的存儲(chǔ)類(lèi)實(shí)例
- JavaScript操作cookie類(lèi)實(shí)例
- javascript操作Cookie(設(shè)置、讀取、刪除)方法詳解
- javascript針對(duì)cookie的基本操作實(shí)例詳解
相關(guān)文章
微信小程序 子級(jí)頁(yè)面返回父級(jí)并把子級(jí)參數(shù)帶回父級(jí)實(shí)現(xiàn)方法
這篇文章主要介紹了微信小程序 子級(jí)頁(yè)面返回父級(jí)并把子級(jí)參數(shù)帶回父級(jí)實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08javascript Array對(duì)象使用小結(jié)
數(shù)組是一段線性分配的內(nèi)存,它通過(guò)整數(shù)去計(jì)算偏移并訪問(wèn)其中的元素。數(shù)組是很快的數(shù)據(jù)結(jié)構(gòu),但不幸的是,Javascript并沒(méi)有像這種數(shù)組一樣的數(shù)據(jù)結(jié)構(gòu)。2009-12-12微信小程序圖片加載失敗時(shí)替換為默認(rèn)圖片的方法
這篇文章主要介紹了微信小程序圖片加載失敗時(shí)替換為默認(rèn)圖片的方法,本文分情況通過(guò)實(shí)例代碼給大家講解,需要的朋友可以參考下2019-12-12javascript類(lèi)型系統(tǒng) Window對(duì)象學(xué)習(xí)筆記
這篇文章主要介紹了javascript類(lèi)型系統(tǒng)之Window對(duì)象,整理關(guān)于Window對(duì)象的學(xué)習(xí)筆記,感興趣的小伙伴們可以參考一下2016-01-01javascript實(shí)現(xiàn)的一個(gè)自定義長(zhǎng)度的文本自動(dòng)換行的函數(shù)。
javascript實(shí)現(xiàn)的一個(gè)自定義長(zhǎng)度的文本自動(dòng)換行的函數(shù)。...2007-08-08JavaScript操作 url 中 search 部分方法函數(shù)
這篇文章主要介紹了JavaScript操作 url 中 search 部分方法函數(shù)的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06