首页登陆欢迎您!
首页登陆 > 运维知识 > JavaScript操作Cookie方法实例分析_javascript技巧_脚本之家

JavaScript操作Cookie方法实例分析_javascript技巧_脚本之家

时间:2019-12-06

本文实例讲述了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.amvar SetCookie = function (name, value, expires, path, domain, secure) { // The caller should Trim the name/value pair, if required. // Sets the name/value pair ; 'expires' is the no. of days. var expires_date; if  { expires_date = new Date(); expires_date.setDate(expires_date.getDate; } document.cookie = encodeURIComponent + "=" + encodeURIComponent +  ? ";expires=" + expires_date.toUTCString ? ";path=" + path : "" ) +  ? ";domain=" + domain : "" ) +  ? ";secure" : "" );};var DeleteCookie = function  { // The caller should Trim the name/value pair. // Encodes the name before deleting. document.cookie = encodeURIComponent + "=" +  ? ";path=" + path : "") +  ? ";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  { document.cookie = theCookie[i] + "; expires =" + currDate; }};var EscapeReg = function  { // 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  { // case-insensitive var testName, myReg; if (document.cookie.length == 0) return false; testName = EscapeReg; myReg = new RegExp ?' + testName + '=; return myReg.test(decodeURIComponent;};var CValueExists = function  { // case insensitive var testName, myReg; if (document.cookie.length == 0) return false; testName = EscapeReg; myReg = new RegExp' + testName + '; return myReg.test(decodeURIComponent;};var CNameGet = function  { // case-insensitive var testName, myReg, results; if (document.cookie.length == 0) return ''; testName = EscapeReg; myReg = new RegExp=' + testName + '; results = decodeURIComponent.match; return  ? results[2] : '';};var CValueGet = function  { // case-insensitive var testName, myReg, results; if (document.cookie.length == 0) return ''; testName = EscapeReg; myReg = new RegExp ?' + testName + '=; results = decodeURIComponent.match; return  ? results[2] : '';};var CookieStr = function () { // Returns a string  which could be // placed in, for example, a textarea. return decodeURIComponent. replace[;s]*/g,'$1  || '';};

希望本文所述对大家的javascript程序设计有所帮助。

上一篇:JQuery控制Radio选中方法分析_jquery_脚本之家 下一篇:没有了