Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #39779

Re: Create cookie from ID of <a> tag

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Create cookie from ID of <a> tag
Date 2021-03-26 19:35 +0100
Organization PointedEars Software (PES)
Message-ID <4641170.31r3eYUQgx@PointedEars.de> (permalink)
References (4 earlier) <48e39d8c-ec2d-4759-8ddd-8ac063e510c3n@googlegroups.com> <cca12429-db8b-4747-990f-0f9e2fedba5dn@googlegroups.com> <s3k5e5$aot$1@dont-email.me> <2578953.mvXUDI8C0e@PointedEars.de> <slrns5s5ue.49m.jon+usenet@raven.unequivocal.eu>

Show all headers | View raw


Jon Ribbens wrote:

> On 2021-03-26, Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote:
>> That is *very* inefficient and error-prone.  Consider this:
>>
>>   function readCookie (name)
>>   {
>>     return (
>>       (
>>         document.cookie.split(';')
>>           .map((cookie) => cookie.split('='))
>>           .filter((cookie) => cookie[0] == name) || []
>>       )[0] || []
>>     )[1];
>>   }
> 
> Bear in mind this will fail on cookies whose values contain an '='.

It will NOT fail, but it will return an incomplete value.

It is possible indeed for a read cookie value to contain an unescaped “=” 
character:

  document.cookie = 'foo=bar=baz';

creates a session cookie with the name “foo” and the value “bar=baz”, and 
after that “document.cookie” contains the substring “bar=baz” unescaped (in 
Chromium).

Thank you for pointing that out.

It is recommended to use the encodeURIComponent() function on the value to 
compatibly avoid storing this and other problematic/forbidden cookie values:

  document.cookie = 'foo=' + encodeURIComponent('bar=baz');

stores the value “bar%3Dbaz”.

Correspondingly, decodeURIComponent() should be used then when reading 
cookie values:

<https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie>

> In Python this is trivial to solve by using split('=', 1) but
> unfortunately in JavaScript the limit parameter is almost useless.

It is true that it cannot be simply used for this:

<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split>

The problem can still be solved simply by splitting on '=', but joining the 
remaining array elements:

  function readCookie (name)
  {
    return (
      (
        document.cookie.split(';')
          .map((cookie) => cookie.split('='))
          .filter((cookie) => cookie[0] == name) || []
      )[0] || []
    ).slice(1).join('=');
  }

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Create cookie from ID of <a> tag Daniele Ferrarese <daniele.seversrl@gmail.com> - 2021-03-24 09:36 -0700
  Re: Create cookie from ID of <a> tag The Natural Philosopher <tnp@invalid.invalid> - 2021-03-24 17:54 +0000
    Re: Create cookie from ID of <a> tag Daniele Ferrarese <daniele.seversrl@gmail.com> - 2021-03-25 01:05 -0700
      Re: Create cookie from ID of <a> tag The Natural Philosopher <tnp@invalid.invalid> - 2021-03-25 09:13 +0000
        Re: Create cookie from ID of <a> tag Daniele Ferrarese <daniele.seversrl@gmail.com> - 2021-03-25 02:21 -0700
          Re: Create cookie from ID of <a> tag Daniele Ferrarese <daniele.seversrl@gmail.com> - 2021-03-25 03:23 -0700
            Re: Create cookie from ID of <a> tag Daniele <webmaster@appliserver.com> - 2021-03-26 09:19 +0100
              Re: Create cookie from ID of <a> tag Daniele Ferrarese <daniele.seversrl@gmail.com> - 2021-03-26 01:57 -0700
              Re: Create cookie from ID of <a> tag Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-03-26 17:33 +0100
                Re: Create cookie from ID of <a> tag Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-03-26 17:20 +0000
                Re: Create cookie from ID of <a> tag Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-03-26 19:35 +0100
                Re: Create cookie from ID of <a> tag Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-03-26 23:22 +0100
                Re: Create cookie from ID of <a> tag "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-03-26 15:39 -0700
                Re: Create cookie from ID of <a> tag Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-03-31 00:58 +0200
                Re: Create cookie from ID of <a> tag "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-03-30 19:58 -0700
                Re: Create cookie from ID of <a> tag Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-04-01 10:09 +0200
                Re: Create cookie from ID of <a> tag "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-04-01 06:11 -0700
                Re: Create cookie from ID of <a> tag Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-04-01 19:34 +0200
              Re: Create cookie from ID of <a> tag The Natural Philosopher <tnp@invalid.invalid> - 2021-03-26 17:10 +0000
                Re: Create cookie from ID of <a> tag Daniele <webmaster@appliserver.com> - 2021-03-27 12:16 +0100
                Re: Create cookie from ID of <a> tag The Natural Philosopher <tnp@invalid.invalid> - 2021-03-27 13:25 +0000
                Re: Create cookie from ID of <a> tag "Mark-" <nospam@mapson.xyz> - 2021-03-31 13:26 +0000
              Re: Create cookie from ID of <a> tag Daniele <webmaster@appliserver.com> - 2021-03-27 12:08 +0100
  Re: Create cookie from ID of <a> tag Man From a city with name Viljandi <ya12983@mail.com> - 2021-03-28 06:26 -0700

csiph-web