Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/477.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript-读取cookie值,如果是字符串的一部分,则触发函数=_Javascript_Cookies - Fatal编程技术网

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

javascript-读取cookie值,如果是字符串的一部分,则触发函数=,javascript,cookies,Javascript,Cookies,我有一块饼干- LLBVAT cookie中的字符串是(可以更改,但结构将保持不变): 所以我知道我需要读取cookie,然后对返回的字符串运行函数 A0A64A06500000B754E9DD10B1381:0:false:false:99:9999:99:false:0:-1:0:0:0:EMAILPCD:1319094000000:82 我需要在第二个false(第四个值)上触发一个函数 任何帮助都将不胜感激。谢谢 是否可以使用更少的代码: function getCookie(name

我有一块饼干-

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;
}
函数getCookie(名称){ 变量nameEQ=name+“=”; var ca=document.cookie.split(“;”); 对于(变量i=0;i拆分
上的值:

var trigger = "A0A64A06500000B754E9DD10B1381:0:false:false:99:9999:99:false:0:-1:0:0:0:EMAILPCD:1319094000000:82".split(':')[3] === "false";
if (trigger) func();
比较非常重要,因为
如果(“false”)
是真实的


从他那里得到一块饼干

那么:

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

拆分
上的值:

var trigger = "A0A64A06500000B754E9DD10B1381:0:false:false:99:9999:99:false:0:-1:0:0:0:EMAILPCD:1319094000000:82".split(':')[3] === "false";
if (trigger) func();
比较非常重要,因为
如果(“false”)
是真实的


从他那里得到一块饼干

那么:

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'
}
你可以用这个

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(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";
}
你可以用这两种方法中的任何一种。然后,正如其他人所说,将结果传递给一个方法,该方法拆分字符串并根据条件调用另一个字符串

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开发者代码的小更新: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; 

},

上述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; 

},

字符串将更改,因此我实际上需要读取cookie字符串将更改,因此我实际上需要读取cookie