如何查找以字符串“ word”开头的cookie并使用char#从字符串中提取信息 [英] How to find cookies which begin with string "word" and extract information from string with char #

查看:92
本文介绍了如何查找以字符串“ word”开头的cookie并使用char#从字符串中提取信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含值的cookie,并且在值之间加上了#,以便以后可以从cookie中提取数据。



现在,我需要一种搜索​​以某些单词开头的cookie的方式,让我们说 word,当我发现cookie时,我想从中提取单词并作为很遗憾,char#是指示符,char%是最后一个字符指示符。

  var name = word + + n1 + + n2 + + n3; 
var值= v1 +# + v2 +# + v3 +# + v4 +%;

name是cookie名称,值是cookie值。



我需要一个函数,该函数将查找所有以 word开头的cookie,并在找到时从4个不同变量的cookie值中提取v1 v2 v3和v4。 / p>

我必须查看所有cookie名称,并且cookie名称是否以



word这样的单词开头是字符串的其余部分。



这是第一部分。



第二部分是当我们找到具有该名称的cookie时,现在从以#字符串分隔的cookie值中,我们需要分离4个变量。



现在是否清楚我需要什么?

解决方案

两个帮助程序功能。



一个将 document.cookie 字符串解码为具有键/值的对象的

 函数decodeCookie(){
var cookieParts = document.cookie.split(;),
cookies = {};

for(var i = 0; i< cookieParts.length; i ++){
var name_value = cookieParts [i],
equals_pos = name_value.indexOf( = ),
name = unescape(name_value.slice(0,equals_pos)).trim(),
value = unescape(name_value.slice(equals_pos + 1));

cookie [: +名称] =值;
}
个返回cookie;
}

一个搜索该对象并找到以某个数字开头的第一个值的对象搜索字词:

 函数findCookieByName(searchWord){
var cookies = encodeCookie();

用于(cookie中的名称){
var value = cookies [name];
if(name.indexOf(: + searchWord)== 0){
返回值;
}
}
}

您可以这样做:

  var value = findCookieByName( word); 

if(value){
var piece = value.split(#); //>值数组
}






PS:我在cookie名称前加上来防止与对象的内置属性发生冲突。例如:您可以将Cookie命名为 __ proto __ ,但这不适用于JavaScript对象。因此,我存储所有带有前缀的cookie名称。


I have cookies with values in it, and between values I put # so I can later extract data from cookies.

Now I need a way to search for cookies which begin with some word let say "word", and when I find that cookies I want to extract word from it and as I sad char # is indicator for that and char % is indicator for last char.

var name = "word" + "" + n1 + "" + n2 + "" + n3;
var value = v1 + "#" + v2 + "#" + v3 + "#" + v4 + "%";

name is cookie name, and value is cookie value.

I need function which will look for all cookies which begin with "word" and when find it, from cookie value extract v1 v2 v3 and v4 in 4 different variables.

I have to look all cookies names and if cookie name begin with "word" like

"word and here is the rest of the string".

That is first part.

Second part is when we find cookies with that name, now from cookie value which is string separated with # we need to separate 4 variables.

Is it now clear what I need?

解决方案

Two helper functions.

One that decodes the document.cookie string into an object with keys/values:

function decodeCookie() {
  var cookieParts = document.cookie.split(";"), 
  cookies = {};

  for (var i = 0; i < cookieParts.length; i++) {
    var name_value = cookieParts[i],
        equals_pos = name_value.indexOf("="),
        name       = unescape( name_value.slice(0, equals_pos) ).trim(),
        value      = unescape( name_value.slice(equals_pos + 1) );

    cookies[":" + name] = value;
  }
  return cookies;
}

one that searches through this object and finds the first value that starts with a certain search word:

function findCookieByName(searchWord) {
  var cookies = decodeCookie();

  for (name in cookies) {
    var value = cookies[name];
    if (name.indexOf(":" + searchWord) == 0) {
      return value;
    }
  }
}

So you can do:

var value = findCookieByName("word");

if (value) {
  var pieces = value.split("#"); // > array of values
}


P.S.: I prepend the cookie name with ":" to prevent clashes with built-in properties of objects. For example: You can name your cookie __proto__ but that wouldn't work too well with JavaScript objects. Hence I store all cookie names with a prefix.

这篇关于如何查找以字符串“ word”开头的cookie并使用char#从字符串中提取信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆