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

标签 javascript cookies

我有一个 cookies -

LLB增值税

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;
}

最佳答案

拆分上的值:

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") 为 true

<小时/>

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();

关于javascript - 读取cookie值,如果字符串的一部分=则触发函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7827306/

相关文章:

java - 使用 App Engine url 获取发布用户凭据时遇到问题

javascript - 如何在同构应用程序中通过 Node 获取传递请求 cookie?

Javascript,对象原型(prototype) - 避免编写完整路径

javascript - 根据cookie值打开iframe

javascript - 将鼠标悬停在标记上时不要更改鼠标图标

javascript - 如何读取 Node 中的两个文件并检查两个文件中是否存在单词

java - cookie 被阻止时 JCaptcha 失败

javascript - 在 Shopify 上更改或刷新页面时货币会恢复

javascript - 使用 &lt;title&gt; 中的字符串更改 div 中的字符串

javascript - 无法强制页面在 Chrome 刷新时滚动到顶部