读取cookie值,如果字符串的一部分=触发函数
read cookie value, trigger function if part of string =
我有一个饼干-
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;
}
拆分:
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();
你可以使用
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'
}
您可以使用jQuery插件或Plain Old Javascript进行cookie管理。从那里,如其他人所说,将结果传递给一个方法,该方法拆分字符串并根据条件调用另一个。
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";
}
上面提到的Mozilla开发人员代码的小更新:文档。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;
}
相关文章:
- 如何知道找到的单词是字符串的一部分还是JavaScript中的函数调用
- c:对于JavaScript函数的一部分时的每个问题
- 将数组变量传递给函数的下一部分
- 如何使样式元素的一部分成为函数?(IE:颜色:$random而不是颜色:红色)
- 粒子 JS – 调用作为对象一部分的函数
- 如何在 for 循环中维护迭代变量的原始值,该循环设置事件调用,其中迭代值是函数的一部分
- 警报和确认函数是内置在 JavaScript 中,还是 DOM 的一部分
- 是否可以将映射集合名称的名称作为MongoDB映射函数中键的一部分
- 如何在JavaScript中重写重载函数的一部分
- 如何将变量传递给属于函数调用参数一部分的函数
- 全局javascript变量在对象构造函数的一部分中变得未定义
- 将json字符串值的一部分传递给jquery函数
- 为什么在构造函数中注释/取消注释alert()会将变量切换为obj实例的一部分
- 为什么有些函数是全局的,而另一些函数是对象的一部分
- 如何使用函数参数作为变量的一部分
- 为什么console.log()和alert()函数实际上不是Javascript的一部分
- Ajax函数作为javascript比较的一部分
- 使用匿名函数作为javascript对象的一部分
- Javascript,试图将函数的一部分写入主体
- 读取cookie值,如果字符串的一部分=触发函数