javascript - 读取cookie值,如果string =的一部分则触发函数

时间:2011-10-19 19:53:58

标签: javascript cookies

我有一个cookie -

LLBVAT

并且cookie中的字符串是(可以更改,但结构将保持不变):

所以我知道我需要读取cookie,然后在返回的字符串上运行一个函数。

A0A64A06500000B754E9DD10B1381:0:false:false:99:9999:99:false:0:-1:0:0:0:EMAILPCD:1319094000000:82

我需要在第二个false(第四个值)上触发一个函数。

任何帮助将不胜感激。感谢。

是否可以使用更少的代码:

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

4 个答案:

答案 0 :(得分:3)

:

上的值拆分
var trigger = "A0A64A06500000B754E9DD10B1381:0:false:false:99:9999:99:false:0:-1:0:0:0:EMAILPCD:1319094000000:82".split(':')[3] === "false";
if (trigger) func();

比较非常重要,因为if ("false")是真实的


https://developer.mozilla.org/en/DOM/document.cookie

获取Cookie
docCookies = {
  getItem: function (sKey) {
    if (!sKey || !this.hasItem(sKey)) { return null; }
    return unescape(document.cookie.replace(new RegExp("(?:^|.*;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*((?:[^;](?!;))*[^;]?).*"), "$1"));
  },
  /**
  * docCookies.setItem(sKey, sValue, vEnd, sPath, sDomain, bSecure)
  *
  * @argument sKey (String): the name of the cookie;
  * @argument sValue (String): the value of the cookie;
  * @optional argument vEnd (Number, String, Date Object or null): the max-age in seconds (e.g., 31536e3 for a year) or the
  *  expires date in GMTString format or in Date Object format; if not specified it will expire at the end of session; 
  * @optional argument sPath (String or null): e.g., "/", "/mydir"; if not specified, defaults to the current path of the current document location;
  * @optional argument sDomain (String or null): e.g., "example.com", ".example.com" (includes all subdomains) or "subdomain.example.com"; if not
  * specified, defaults to the host portion of the current document location;
  * @optional argument bSecure (Boolean or null): cookie will be transmitted only over secure protocol as https;
  * @return undefined;
  **/
  setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
    if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/.test(sKey)) { return; }
    var sExpires = "";
    if (vEnd) {
      switch (typeof vEnd) {
        case "number": sExpires = "; max-age=" + vEnd; break;
        case "string": sExpires = "; expires=" + vEnd; break;
        case "object": if (vEnd.hasOwnProperty("toGMTString")) { sExpires = "; expires=" + vEnd.toGMTString(); } break;
      }
    }
    document.cookie = escape(sKey) + "=" + escape(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
  },
  removeItem: function (sKey) {
    if (!sKey || !this.hasItem(sKey)) { return; }
    var oExpDate = new Date();
    oExpDate.setDate(oExpDate.getDate() - 1);
    document.cookie = escape(sKey) + "=; expires=" + oExpDate.toGMTString() + "; path=/";
  },
  hasItem: function (sKey) { return (new RegExp("(?:^|;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie); }
};

// docCookies.setItem("test1", "Hello world!");
// docCookies.setItem("test2", "Hello world!", new Date(2020, 5, 12));
// docCookies.setItem("test3", "Hello world!", new Date(2027, 2, 3), "/blog");
// docCookies.setItem("test4", "Hello world!", "Sun, 06 Nov 2022 21:43:15 GMT");
// docCookies.setItem("test5", "Hello world!", "Tue, 06 Dec 2022 13:11:07 GMT", "/home");
// docCookies.setItem("test6", "Hello world!", 150);
// docCookies.setItem("test7", "Hello world!", 245, "/content");
// docCookies.setItem("test8", "Hello world!", null, null, "example.com");
// docCookies.setItem("test9", "Hello world!", null, null, null, true);

// alert(docCookies.getItem("test1"));

那么:

if (docCookies.getItem("LLBVAT").split(':')[3] === "false") triggerFunction();

答案 1 :(得分:0)

你可以用这个

function getTheResult(){
  //you can get this value as parameter in this function
  var val="A0A64A06500000B754E9DD10B1381:0:false:false:99:9999:99:false:0:-1:0:0:0:EMAILPCD:1319094000000:82";    

  //this final result will be as below by splitting the string
  var theresult =val.split(':')[3];    

  return theresult; //this will return 'false'
}

答案 2 :(得分:0)

您可以使用jQuery plugin or Plain Old Javascript for cookie management。从那里开始,正如其他人所说,将结果传递给一个方法,该方法将字符串拆分并根据条件调用另一个字符串。

jQuery(document).ready(function(){
    var cookieValue = jQuery.cookie("LLBVAT");
    var result = process(cookieValue);
    if(result)
    { //do stuff }
});

function process(cookie){
   var result = cookie.split(':')[3];    
   return result === "false";
}

答案 3 :(得分:0)

上面提到的Mozilla开发人员代码的小更新:document.cookie只返回域和路径与当前URL匹配的cookie(由于安全限制)。要读取为其他路径创建的cookie,您可以使用以下解决方法:

  getItem: function (sKey, sPath, sDomain) {
if (!sKey) {
  return "";
 }
var allCookie;
if (!sDomain && !sPath) {
  allCookie = document.cookie;
} else {
  var iframe = document.createElement("IFRAME");
  iframe.setAttribute("src", (sDomain ? sDomain : "") + (sPath ? sPath : ""));
  document.documentElement.appendChild(iframe);
  allCookie = iframe.contentDocument.cookie;
  iframe.parentNode.removeChild(iframe);
  iframe = null;
}
var sValue = decodeURIComponent(allCookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
return sValue; 

},