Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #35845
| Path | csiph.com!aioe.org!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
| Newsgroups | comp.lang.javascript |
| Subject | Re: Accessing the value of a cookie |
| Date | Wed, 21 Nov 2018 12:37:48 +0000 |
| Organization | A noiseless patient Spider |
| Lines | 37 |
| Message-ID | <87lg5md2sj.fsf@bsb.me.uk> (permalink) |
| References | <f11ea23c-05c7-4b52-8f52-c66912ac1e4e@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain |
| Injection-Info | reader01.eternal-september.org; posting-host="54137f4f1bd58961f89227006ff320d7"; logging-data="890"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19VJkCP3LyAHphe7sEAUyVNfNl4cYEnPcY=" |
| Cancel-Lock | sha1:wOLhObl+jG5uqkvWHE246+Bc3Kw= sha1:0p5wLTuM4YWLeFFc+qPFio+5oeo= |
| X-BSB-Auth | 1.863c6d44d555d23f453a.20181121123748GMT.87lg5md2sj.fsf@bsb.me.uk |
| Xref | csiph.com comp.lang.javascript:35845 |
Show key headers only | View raw
groovee@cyberdude.com writes:
> I was trying to check whether a cookie is set to a particular value or
> not (on the client side, in JS), and found this code on MDN:
>
> if (document.cookie.split(';').filter(
> (item) => item.includes('reader=1')).length) {
> console.log('The cookie "reader" has "1" for value')
> }
> What I'd like to know is - what's the ".length" at the end for? I'm
> trying to get the VALUE of the cookie, why would it's *length* be
> required?
You are trying to do something different, so you won't want to know how
many items get past the filter, which is what the length test is for.
The original code (which you altered) is intended to find out if a key
exists, not what value it has.
You changed "reader=" to "reader=1", but that might not do do what you
think it does because the inner "includes" test will succeed when
reader=11 is in the string. Mind you, the original code is also a bit
dodgy since it accepts "xyzreader=" as well as "reader=".
If you need to get the key=value pairs you should split on /;[ \t]*/,
and if you need to test for a specific key you should check that the
pair starts with the required key. Mind you, a regexp could give you a
quick check for existence:
document.cookie.match(/(:?^|;[ \t]*)reader=/)
(Don't rely on this being safe -- to get the right regexp you'd need to
(a) read the spec, and (b) also know if there are any user agents out
there that don't conform!)
--
Ben.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Accessing the value of a cookie groovee@cyberdude.com - 2018-11-20 23:33 -0800
Re: Accessing the value of a cookie JJ <jj4public@vfemail.net> - 2018-11-21 16:03 +0700
Re: Accessing the value of a cookie Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-11-21 12:37 +0000
Re: Accessing the value of a cookie groovee@cyberdude.com - 2018-11-22 00:56 -0800
Re: Accessing the value of a cookie Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-11-22 12:22 +0000
Re: Accessing the value of a cookie Julio Di Egidio <julio@diegidio.name> - 2018-11-24 06:11 -0800
csiph-web