简体   繁体   English

如何使用 javascript 从 cookie 创建和读取值?

[英]How do I create and read a value from cookie with javascript?

如何在 JavaScript 中创建和读取 cookie 中的值?

Here are functions you can use for creating and retrieving cookies.以下是可用于创建和检索 cookie 的功能。

function createCookie(name, value, days) {
    var expires;
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toGMTString();
    }
    else {
        expires = "";
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) {
                c_end = document.cookie.length;
            }
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

Minimalistic and full featured ES6 approach:简约且功能齐全的 ES6 方法:

const setCookie = (name, value, days = 7, path = '/') => {
  const expires = new Date(Date.now() + days * 864e5).toUTCString()
  document.cookie = name + '=' + encodeURIComponent(value) + '; expires=' + expires + '; path=' + path
}

const getCookie = (name) => {
  return document.cookie.split('; ').reduce((r, v) => {
    const parts = v.split('=')
    return parts[0] === name ? decodeURIComponent(parts[1]) : r
  }, '')
}

const deleteCookie = (name, path) => {
  setCookie(name, '', -1, path)
}

JQuery Cookies jQuery Cookie

or plain Javascript:或纯 Javascript:

function setCookie(c_name,value,exdays)
{
   var exdate=new Date();
   exdate.setDate(exdate.getDate() + exdays);
   var c_value=escape(value) + ((exdays==null) ? "" : ("; expires="+exdate.toUTCString()));
   document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name)
{
   var i,x,y,ARRcookies=document.cookie.split(";");
   for (i=0; i<ARRcookies.length; i++)
   {
      x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
      y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
      x=x.replace(/^\s+|\s+$/g,"");
      if (x==c_name)
      {
        return unescape(y);
      }
   }
}

ES7, using a regex for get(). ES7,对 get() 使用正则表达式。 Based on MDN基于MDN

const Cookie = {
    get: name => {
        let c = document.cookie.match(`(?:(?:^|.*; *)${name} *= *([^;]*).*$)|^.*$`)[1]
        if (c) return decodeURIComponent(c)
    },
    set: (name, value, opts = {}) => {
        /*If options contains days then we're configuring max-age*/
        if (opts.days) {
            opts['max-age'] = opts.days * 60 * 60 * 24;

            /*Deleting days from options to pass remaining opts to cookie settings*/
            delete opts.days 
        }

        /*Configuring options to cookie standard by reducing each property*/
        opts = Object.entries(opts).reduce(
            (accumulatedStr, [k, v]) => `${accumulatedStr}; ${k}=${v}`, ''
        )

        /*Finally, creating the key*/
        document.cookie = name + '=' + encodeURIComponent(value) + opts
    },
    delete: (name, opts) => Cookie.set(name, '', {'max-age': -1, ...opts}) 
    // path & domain must match cookie being deleted 
}

Cookie.set('user', 'Jim', {path: '/', days: 10}) 
// Set the path to top level (instead of page) and expiration to 10 days (instead of session)

Usage - Cookie.get(name, value [, options]):用法 - Cookie.get(name, value [, options]):
options supports all standard cookie options and adds "days": options 支持所有标准 cookie 选项并添加“天数”:

  • path : '/' - any absolute path. path : '/' - 任何绝对路径。 Default : current document location,默认值:当前文档位置,
  • domain : 'sub.example.com' - may not start with dot. domain : 'sub.example.com' - 不能以点开头。 Default : current host without subdomain.默认值:没有子域的当前主机。
  • secure : true - Only serve cookie over https.安全:true - 仅通过 https 提供 cookie。 Default : false.默认值:假。
  • days : 2 - days till cookie expires. days : 2 - 直到 cookie 过期的天数。 Default : End of session.默认值:会话结束。
    Alternative ways of setting expiration:设置过期的替代方法:
    • expires : 'Sun, 18 Feb 2018 16:23:42 GMT' - date of expiry as a GMT string. expires : 'Sun, 18 Feb 2018 16:23:42 GMT' - 作为 GMT 字符串的到期日期。
      Current date can be gotten with: new Date(Date.now()).toUTCString()当前日期可以通过:new Date(Date.now()).toUTCString()
    • 'max-age' : 30 - same as days, but in seconds instead of days. 'max-age' : 30 - 与天数相同,但以秒为单位而不是天数。

Other answers use "expires" instead of "max-age" to support older IE versions.其他答案使用“expires”而不是“max-age”来支持较旧的 IE 版本。 This method requires ES7, so IE7 is out anyways (this is not a big deal).这种方法需要 ES7,所以无论如何 IE7 都出来了(这没什么大不了的)。

Note: Funny characters such as "=" and "{:}" are supported as cookie values, and the regex handles leading and trailing whitespace (from other libs).注意: cookie 值支持诸如“=”和“{:}”之类的有趣字符,并且正则表达式处理前导和尾随空格(来自其他库)。
If you would like to store objects, either encode them before and after with and JSON.stringify and JSON.parse, edit the above, or add another method.如果您想存储对象,请在使用 JSON.stringify 和 JSON.parse 之前和之后对它们进行编码,编辑上述内容,或添加其他方法。 Eg:例如:

Cookie.getJSON = name => JSON.parse(Cookie.get(name))
Cookie.setJSON = (name, value, opts) => Cookie.set(name, JSON.stringify(value), opts);

Mozilla created a simple framework for reading and writing cookies with full unicode support along with examples of how to use it. Mozilla 创建了一个简单的框架,用于读取和写入完全支持 unicode 的 cookie,以及如何使用它的示例。

Once included on the page, you can set a cookie:一旦包含在页面中,您就可以设置 cookie:

docCookies.setItem(name, value);

read a cookie:读取 cookie:

docCookies.getItem(name);

or delete a cookie:或删除 cookie:

docCookies.removeItem(name);

For example:例如:

// sets a cookie called 'myCookie' with value 'Chocolate Chip'
docCookies.setItem('myCookie', 'Chocolate Chip');

// reads the value of a cookie called 'myCookie' and assigns to variable
var myCookie = docCookies.getItem('myCookie');

// removes the cookie called 'myCookie'
docCookies.removeItem('myCookie');

See more examples and details on Mozilla's document.cookie page .Mozilla 的 document.cookie 页面上查看更多示例和详细信息。

A version of this simple js file is on github .这个简单的 js 文件的一个版本 在 github 上

I've used accepted answer of this thread many times already.我已经多次使用这个线程的接受答案。 It's great piece of code: Simple and usable.这是一段很棒的代码:简单且可用。 But I usually use babel and ES6 and modules, so if you are like me, here is code to copy for faster developing with ES6但我通常使用babel和 ES6 和模块,所以如果你和我一样,这里是复制代码以便更快地使用 ES6 开发

Accepted answer rewritten as module with ES6:接受的答案用 ES6 重写为模块:

export const createCookie = ({name, value, days}) => {
  let expires;
  if (days) {
    let date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    expires = '; expires=' + date.toUTCString();
  } else {
    expires = '';
  }
  document.cookie = name + '=' + value + expires + '; path=/';
};

export const getCookie = ({name}) => {
  if (document.cookie.length > 0) {
    let c_start = document.cookie.indexOf(name + '=');
    if (c_start !== -1) {
      c_start = c_start + name.length + 1;
      let c_end = document.cookie.indexOf(';', c_start);
      if (c_end === -1) {
        c_end = document.cookie.length;
      }
      return unescape(document.cookie.substring(c_start, c_end));
    }
  }
  return '';
};

And after this you can simply import it as any module (path of course may vary):在此之后,您可以简单地将其作为任何模块导入(路径当然可能会有所不同):

import {createCookie, getCookie} from './../helpers/Cookie';

For those who need save objects like {foo: 'bar'}, I share my edited version of @KevinBurke's answer.对于那些需要像 {foo: 'bar'} 这样保存对象的人,我分享了我编辑过的@KevinBurke 答案的版本。 I've added JSON.stringify and JSON.parse, that's all.我已经添加了 JSON.stringify 和 JSON.parse,仅此而已。

cookie = {

    set: function (name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expires=" + date.toGMTString();
        }
        else
            var expires = "";
        document.cookie = name + "=" + JSON.stringify(value) + expires + "; path=/";
    },

    get : function(name){
        var nameEQ = name + "=",
            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  JSON.parse(c.substring(nameEQ.length,c.length));
        }

        return null;
    }

}

So, now you can do things like this:所以,现在你可以做这样的事情:

cookie.set('cookie_key', {foo: 'bar'}, 30);

cookie.get('cookie_key'); // {foo: 'bar'}

cookie.set('cookie_key', 'baz', 30);

cookie.get('cookie_key'); // 'baz'

Here's a code to Get, Set and Delete Cookie in JavaScript .这是在 JavaScript 中获取、设置和删除 Cookie的代码。

 function getCookie(name) { name = name + "="; var cookies = document.cookie.split(';'); for(var i = 0; i <cookies.length; i++) { var cookie = cookies[i]; while (cookie.charAt(0)==' ') { cookie = cookie.substring(1); } if (cookie.indexOf(name) == 0) { return cookie.substring(name.length,cookie.length); } } return ""; } function setCookie(name, value, expirydays) { var d = new Date(); d.setTime(d.getTime() + (expirydays*24*60*60*1000)); var expires = "expires="+ d.toUTCString(); document.cookie = name + "=" + value + "; " + expires; } function deleteCookie(name){ setCookie(name,"",-1); }

Source: http://mycodingtricks.com/snippets/javascript/javascript-cookies/来源:http: //mycodingtricks.com/snippets/javascript/javascript-cookies/

Performance benchmark性能基准

Comparison of ES6 versions of some popular getCookie functions (with my improvements): https://www.measurethat.net/Benchmarks/Show/16012/5/getcookie-for-vs-forof-vs-indexof-vs-find-vs-reduce一些流行的getCookie函数的ES6版本对比(经过我的改进): https ://www.measurethat.net/Benchmarks/Show/16012/5/getcookie-for-vs-forof-vs-indexof-vs-find-vs -减少

TL;DR : for...of version seams to be fastest for real-life cookies data :) TL;DR : for...of版本接缝对于现实生活中的 cookie 数据来说是最快的 :)

Important: document.cookie can provide duplicated cookie names if there are cookies with the same name for path=/ and current page path (eg. path=/faq ).重要提示:如果path=/和当前页面路径(例如path=/faq )有相同名称的 cookie,则document.cookie可以提供重复的 cookie 名称 But the cookie for the current path will always be the first in the string, so be aware of this when using the reduce() version from the other answer provided here (it returns the last found cookie instead of the first one).但是当前路径的 cookie 将始终是字符串中的第一个,因此在使用此处提供的另一个答案中的reduce()版本时要注意这一点(它返回最后找到的 cookie 而不是第一个)。

Fixed reduce() version is further in my answer.固定reduce()版本在我的回答中更进一步。

For..of version:对于..of 版本:

Fastest for the real-life benchmark data set (10 cookies with long values).现实生活中的基准数据集最快(10 个长值 cookie)。 But performance results are almost the same as with vanilla for loop and with Array.find() , so use which you like :)但是性能结果与 vanilla for循环和Array.find()几乎相同,所以使用你喜欢的:)

function getCookieForOf(name) {
  const nameEQ = name + '=';
  for (const cookie of document.cookie.split('; ')) {
    if (cookie.indexOf(nameEQ) === 0) {
      const value = cookie.substring(nameEQ.length);
      return decodeURIComponent(value); // returns first found cookie
    }
  }
  return null;
}

IndexOf version版本索引

Incredibly fast in the artificial test set of 1000 cookies with short values (because it doesn't create an array with 1000 records).在具有短值的 1000 个 cookie 的人工测试集中令人难以置信地快(因为它不会创建一个包含 1000 条记录的数组)。 To be honest, I consider there could be a bug in the test code that makes this version so crazy fast (if you would find some, pls let me know).老实说,我认为测试代码中可能存在一个错误,使这个版本变得如此疯狂(如果你能找到一些,请告诉我)。 Anyway, it's rather not probable to have 1000 cookies in the real App ;)无论如何,在真正的应用程序中不太可能有 1000 个 cookie ;)

It's slow for the real-world test data set with 10 long cookies.对于具有 10 个长 cookie 的真实测试数据集来说,速度很慢。

function getCookieIndexOf(name) {
  const nameEQ = name + '=';
  const cookies = document.cookie;
  const cookieStart = cookies.indexOf(nameEQ);
  if (cookieStart !== -1) {
    const cookieValueStart = cookieStart + nameEQ.length;
    const cookieEnd = cookies.indexOf(';', cookieValueStart);
    const value = cookies.substring(
      cookieValueStart,
      cookieEnd !== -1 ? cookieEnd : undefined
    );
    return decodeURIComponent(value); // returns first found cookie
  }
  return null;
}

Array.find() version Array.find() 版本

function getCookieFind(name) {
  const nameEQ = name + '=';
  const foundCookie = document.cookie
    .split('; ')
    .find(c => c.indexOf(nameEQ) === 0); // returns first found cookie
  if (foundCookie) {
    return decodeURIComponent(foundCookie.substring(nameEQ.length));
  }
  return null;
}

Vanilla, old-school, for-loop version ;)香草,老式,for-loop 版本;)

function getCookieFor(name) {
    const nameEQ = name + "=";
    const ca = cookies.split('; ');
    for(let i=0; i < ca.length; i++) {
        const c = ca[i];
        if (c.indexOf(nameEQ) === 0) {
          const value = c.substring(nameEQ.length);
          return decodeURIComponent(value); // returns first found cookie
        }
    }
    return null;
}

// ES5 version:
function getCookieFor(name) {
    var nameEQ = name + "=";
    var ca = cookies.split('; ');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        if (c.indexOf(nameEQ) === 0) {
          var value = c.substring(nameEQ.length);
          return decodeURIComponent(value); // returns first found cookie
        }
    }
    return null;
}

Array.reduce() version Array.reduce() 版本

My fixed version of this answer from @artnikpro - returns the first found cookie, so works better with duplicated cookie names for the current path (eg path=/faq ) and path=/ .我来自@artnikpro 的这个答案的固定版本 - 返回第一个找到的 cookie,因此对于当前路径(例如path=/faq )和path=/的重复 cookie 名称效果更好。

This version is the slowest one in all performance tests, so IMHO should be avoided.此版本是所有性能测试中最慢的版本,因此应避免使用恕我直言。

function getCookieReduce(name) {
  return document.cookie.split('; ').reduce((r, v) => {
    const [n, ...val] = v.split('='); // cookie value can contain "="
    if(r) return r; // returns first found cookie
    return n === name ? decodeURIComponent(val.join('=')) : r; // returns last found cookie (overwrites)
  }, '');
}

You can run benchmarks by yourself here: https://www.measurethat.net/Benchmarks/Show/16012/5/getcookie-for-vs-forof-vs-indexof-vs-find-vs-reduce您可以在此处自行运行基准测试https ://www.measurethat.net/Benchmarks/Show/16012/5/getcookie-for-vs-forof-vs-indexof-vs-find-vs-reduce


setCookie() TypeScript function setCookie() 打字稿函数

Here is also my version of the function to set a cookie with encodeURIComponent , TypeScript , and SameSite option (which will be required by Firefox soon ):这也是我使用encodeURIComponentTypeScriptSameSite选项设置 cookie 的函数版本( Firefox 很快就会需要):

function setCookie(
  name: string,
  value: string = '',
  days: number | false = false, // session length if not provided
  path: string = '/', // provide an empty string '' to set for current path (managed by a browser)
  sameSite: 'none' | 'lax' | 'strict' = 'lax', // required by Firefox
  isSecure?: boolean
) {
  let expires = '';
  if (days) {
    const date = new Date(
      Date.now() + days * 24 * 60 * 60 * 1000
    ).toUTCString();
    expires = '; expires=' + date;
  }
  const secure = isSecure || sameSite === 'none' ? `; Secure` : '';
  const encodedValue = encodeURIComponent(value);
  document.cookie = `${name}=${encodedValue}${expires}; path=${path}; SameSite=${sameSite}${secure}`;
}

Google Chrome Cookie Storage API谷歌浏览器 Cookie 存储 API

Thanks to @oncode answer it's worth mentioning that the Google Chrome team has proposed some standardization (finally! It's really ridiculous that we still don't have any commonly accepted API for cookies) with asynchronous Cookie Storage API (available in Google Chrome starting from version 87): https://wicg.github.io/cookie-store/感谢@oncode 的回答,值得一提的是,谷歌浏览器团队已经提出了一些标准化(终于!我们仍​​然没有任何普遍接受的 cookie API 真的很荒谬)异步 Cookie 存储 API(谷歌浏览器从版本开始可用) 87): https://wigg.github.io/cookie-store/

Unfortunately, it's still unofficial and isn't even under W3C consideration nor ES proposal: github.com/tc39/proposals不幸的是,它仍然是非官方的,甚至不在 W3C 的考虑范围内,也不在 ES 提案中:github.com/tc39/proposals

Such a shame we still don't have any standard API for cookies...真遗憾,我们仍然没有任何标准的 cookie API……

Fortunately, we have cookie-store polyfill for other browsers as npm package ( gitHub ), which is only 1.7kB Gzipped ;)幸运的是,我们有其他浏览器的cookie-store polyfill 作为npm 包( gitHub ),它只有1.7kB Gzipped ;)

I use this object.我使用这个对象。 Values are encoded, so it's necessary to consider it when reading or writing from server side.值是编码的,因此在从服务器端读取或写入时需要考虑它。

cookie = (function() {

/**
 * Sets a cookie value. seconds parameter is optional
 */
var set = function(name, value, seconds) {
    var expires = seconds ? '; expires=' + new Date(new Date().getTime() + seconds * 1000).toGMTString() : '';
    document.cookie = name + '=' + encodeURIComponent(value) + expires + '; path=/';
};

var map = function() {
    var map = {};
    var kvs = document.cookie.split('; ');
    for (var i = 0; i < kvs.length; i++) {
        var kv = kvs[i].split('=');
        map[kv[0]] = decodeURIComponent(kv[1]);
    }
    return map;
};

var get = function(name) {
    return map()[name];
};

var remove = function(name) {
    set(name, '', -1);
};

return {
    set: set,
    get: get,
    remove: remove,
    map: map
};

})();

I like this one-liner solution for reading cookies in modern JavaScript:我喜欢这种在现代 JavaScript 中读取 cookie 的单行解决方案:

let cookies = Object.fromEntries(document.cookie.split(';').map(i=>i.trim().split('=')));

And now you have a JavaScript Object with keys and values.现在你有了一个带有键和值的 JavaScript 对象。

I've used js-cookie to success.我已经使用js-cookie成功了。

<script src="/path/to/js.cookie.js"></script>
<script>
  Cookies.set('foo', 'bar');
  Cookies.get('foo');
</script>

You can use my cookie ES module for get/set/remove cookie.您可以使用我的cookie ES 模块获取/设置/删除 cookie。

Usage:用法:

In your head tag, include the following code:在您的 head 标签中,包含以下代码:

<script src="https://raw.githack.com/anhr/cookieNodeJS/master/build/cookie.js"></script>

or或者

<script src="https://raw.githack.com/anhr/cookieNodeJS/master/build/cookie.min.js"></script>

Now you can use window.cookie for store user information in web pages.现在您可以使用 window.cookie 在网页中存储用户信息。

cookie.isEnabled() cookie.isEnabled()

Is the cookie enabled in your web browser?您的网络浏览器中是否启用了 cookie?

returns {boolean} true if cookie enabled.

Example例子

if ( cookie.isEnabled() )
    console.log('cookie is enabled on your browser');
else
    console.error('cookie is disabled on your browser');

cookie.set( name, value ) cookie.set(名称,值)

Set a cookie.设置一个cookie。

name: cookie name.
value: cookie value.

Example例子

cookie.set('age', 25);

cookie.get( name[, defaultValue] ); cookie.get(name[, defaultValue]);

get a cookie.得到一个饼干。

name: cookie name.
defaultValue: cookie default value. Default is undefined.
returns cookie value or defaultValue if cookie was not found
Example 例子
var age = cookie.get('age', 25);

cookie.remove( name ); cookie.remove(名称);

Remove cookie.删除饼干。

 name: cookie name.
Example 例子
cookie.remove( 'age' );

Example of usage 使用示例

I use the following functions, which I have written by taking the best I have found from various sources and weeded out some bugs or discrepancies.我使用以下函数,这些函数是我通过从各种来源中找到的最好的东西编写的,并清除了一些错误或差异。

The function setCookie does not have advanced options, just the simple stuff, but the code is easy to understand, which is always a plus:函数 setCookie 没有高级选项,只是一些简单的东西,但是代码很容易理解,这总是一个优点:

function setCookie(name, value, daysToLive = 3650) { // 10 years default
  let cookie = name + "=" + encodeURIComponent(value);
  if (typeof daysToLive === "number") {
    cookie += "; max-age=" + (daysToLive * 24 * 60 * 60);
    document.cookie = cookie + ";path=/";
  }
}
function getCookie(name) {
  let cookieArr = document.cookie.split(";");
  for (let i = 0; i < cookieArr.length; i++) {
    let cookiePair = cookieArr[i].split("=");
    if (name == cookiePair[0].trim()) {
      return decodeURIComponent(cookiePair[1].trim());
    }
  }
  return undefined;
}
function deleteCookie(name) {
  setCookie(name, '', -1);
}

The chrome team has proposed a new way of managing cookies asynchronous with the Cookie Storage API (available in Google Chrome starting from version 87): https://wicg.github.io/cookie-store/ chrome 团队提出了一种与 Cookie Storage API 异步管理 cookie 的新方法(从版本 87 开始在 Google Chrome 中可用): https ://wicg.github.io/cookie-store/

Use it already today with a polyfill for the other browsers: https://github.com/mkay581/cookie-store今天已经将它与其他浏览器的 polyfill 一起使用: https ://github.com/mkay581/cookie-store

// load polyfill
import 'cookie-store';

// set a cookie
await cookieStore.set('name', 'value');
// get a cookie
const savedValue = await cookieStore.get('name');

Simple way to read cookies in ES6.在 ES6 中读取 cookie 的简单方法。

function getCookies() {
    var cookies = {};
    for (let cookie of document.cookie.split('; ')) {
        let [name, value] = cookie.split("=");
        cookies[name] = decodeURIComponent(value);
    }
    console.dir(cookies);
}

Very short ES6 functions using template literals.使用模板文字的非常短的 ES6 函数。 Be aware that you need to encode/decode the values by yourself but it'll work out of the box for simplier purposes like storing version numbers.请注意,您需要自己对值进行编码/解码,但它可以开箱即用,以实现更简单的目的,例如存储版本号。

const getCookie = (cookieName) => {
  return (document.cookie.match(`(^|;) *${cookieName}=([^;]*)`)||[])[2]
}
  
const setCookie = (cookieName, value, days=360, path='/') => {
  let expires = (new Date(Date.now()+ days*86400*1000)).toUTCString();
  document.cookie = `${cookieName}=${value};expires=${expires};path=${path};`
}

const deleteCookie = (cookieName) => {
  document.cookie = `${cookieName}=;expires=Thu, 01 Jan 1970 00:00:01 GMT;path=/;`;
}  

Through a interface similar to sessionStorage and localStorage :通过类似于sessionStoragelocalStorage的接口:

const cookieStorage = {
  getItem: (key) {
    const cookies = document.cookie.split(';')
      .map(cookie => cookie.split('='))
      .reduce(
        (accumulation, [key, value]) => ({...accumulation, [key.trim()]: value}),
        {}
      )
    
    return cookies[key]
  },
  setItem: (key, value) {
    document.cookie = `${key}=${value}`
  },
}

Its usage cookieStorage.setItem('', '') and cookieStorage.getItem('') .它的用法cookieStorage.setItem('', '')cookieStorage.getItem('')

An improved version of the readCookie: readCookie 的改进版本:

function readCookie( name )
{
    var cookieParts = document.cookie.split( ';' )
    ,   i           = 0
    ,   part
    ,   part_data
    ,   value
    ;

    while( part = cookieParts[ i++ ] )
    {
        part_data = part.split( '=' );

        if ( part_data.shift().replace(/\s/, '' ) === name )
        {
            value = part_data.shift();
            break;
        }

    }
    return value;
}

This should break as soon as you have found your cookie value and return its value.一旦你找到你的 cookie 值并返回它的值,这应该会中断。 In my opinion very elegant with the double split.在我看来,双拆分非常优雅。

The replace on the if-condition is a white space trim, to make sure it matches correctly if 条件上的替换是空格修剪,以确保它正确匹配

function setCookie(cname,cvalue,exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires=" + d.toGMTString();
    document.cookie = cname+"="+cvalue+"; "+expires;
}

function getCookie(cname) {
    var name = cname + "=";
    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);
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

function checkCookie() {
    var user=getCookie("username");
    if (user != "") {
        alert("Welcome again " + user);
    } else {
       user = prompt("Please enter your name:","");
       if (user != "" && user != null) {
           setCookie("username", user, 30);
       }
    }
}

I have written simple cookieUtils, it has three functions for creating the cookie, reading the cookie and deleting the cookie.我写了简单的cookieUtils,它有创建cookie、读取cookie和删除cookie三个函数。

var CookieUtils = {
    createCookie: function (name, value, expireTime) {
        expireTime = !!expireTime ? expireTime : (15 * 60 * 1000); // Default 15 min
        var date = new Date();
        date.setTime(date.getTime() + expireTime);
        var expires = "; expires=" + date.toGMTString();
        document.cookie = name + "=" + value + expires + "; path=/";
    },
    getCookie: function (name) {
        var value = "; " + document.cookie;
        var parts = value.split("; " + name + "=");
        if (parts.length == 2) {
            return parts.pop().split(";").shift();
        }
    },
    deleteCookie: function(name) {
        document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
    }
};

Here is the example from w3chools that was mentioned.这是提到的w3chools的示例。

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+ d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

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

A simple read一个简单的阅读

var getCookie = function (name) {
    var valueStart = document.cookie.indexOf(name + "=") + name.length + 1;
    var valueEnd = document.cookie.indexOf(";", valueStart); 
    return document.cookie.slice(valueStart, valueEnd)
}

A cheeky and simple way of reading a cookie could be something like:一种厚颜无耻且简单的读取 cookie 的方法可能是:

let username, id; 
eval(document.cookie); 
console.log(username + ", " + id); // John Doe, 123

This could be used if you know your cookie contains something like: username="John Doe"; id=123;如果您知道您的 cookie 包含以下内容,则可以使用此选项: username="John Doe"; id=123; username="John Doe"; id=123; . . Note that a string would need quotes in the cookie.请注意,字符串需要在 cookie 中使用引号。 Not the recommended way probably, but works for testing/learning.可能不是推荐的方式,但适用于测试/学习。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JavaScript中创建Cookie? - How do i create a cookie in JavaScript? 如何使用JavaScript读取Cookie值? - How to read cookie value with javascript? PHP / JavaScript cookie的行为就像一个指针,因此当我清除它时,我会失去价值; 如何从Cookie中获取价值? - PHP/JavaScript cookie acting like a pointer so when I clear it, I lose the value; how do I grab the value from the cookie? 如何读取跨域图像随附的Cookie的值? - How do I read the value of a cookie that comes with a cross domain image? 如何在Jquery中读取Cookie值并创建另一个具有相同值的Cookie - How to read a cookie value in Jquery and create another cookie with same value 如何将从javascript函数返回的cookie值传递给JSON参数字段? - How do I pass in a cookie value returned from a javascript function to a JSON parameter field? 在 JavaScript 中保存 cookie 值时如何设置路径? - How do I set path while saving a cookie value in JavaScript? 如何从PHP到javascript获取cookie? - How do I get a cookie from PHP to javascript? 如何使用 Javascript 从特定域中删除 cookie? - How do I delete a cookie from a specific domain using Javascript? 如何使用JavaScript中的XMLHttpRequest更改“ Cookie”(在标头字段中)中一个cookie的值? - How do I change the value of one cookie in 'Cookie' (in the header field) with XMLHttpRequest in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM