JavaScript操作Cookie方法實例分析
更新時間:2015年05月27日 17:31:26 作者:不吃皮蛋
這篇文章主要介紹了JavaScript操作Cookie方法,實例分析了javascript針對cookie操作的相關(guān)技巧,需要的朋友可以參考下
本文實例講述了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') || ''; };
希望本文所述對大家的javascript程序設(shè)計有所幫助。
您可能感興趣的文章:
- JavaScript實現(xiàn)cookie的寫入、讀取、刪除功能
- javascript封裝 Cookie 應(yīng)用接口
- JavaScript對Cookie進行讀寫操作實例
- JavaScript中Cookies的相關(guān)使用教程
- javascript實現(xiàn)設(shè)置、獲取和刪除Cookie的方法
- JavaScript使用cookie實現(xiàn)記住賬號密碼功能
- JavaScript實現(xiàn)基于Cookie的存儲類實例
- JavaScript操作cookie類實例
- javascript操作Cookie(設(shè)置、讀取、刪除)方法詳解
- javascript針對cookie的基本操作實例詳解
相關(guān)文章
微信小程序 子級頁面返回父級并把子級參數(shù)帶回父級實現(xiàn)方法
這篇文章主要介紹了微信小程序 子級頁面返回父級并把子級參數(shù)帶回父級實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08javascript類型系統(tǒng) Window對象學(xué)習(xí)筆記
這篇文章主要介紹了javascript類型系統(tǒng)之Window對象,整理關(guān)于Window對象的學(xué)習(xí)筆記,感興趣的小伙伴們可以參考一下2016-01-01javascript實現(xiàn)的一個自定義長度的文本自動換行的函數(shù)。
javascript實現(xiàn)的一個自定義長度的文本自動換行的函數(shù)。...2007-08-08JavaScript操作 url 中 search 部分方法函數(shù)
這篇文章主要介紹了JavaScript操作 url 中 search 部分方法函數(shù)的相關(guān)資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下2016-06-06