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


Groups > comp.lang.javascript > #39785

Re: Create cookie from ID of <a> tag

From Daniele <webmaster@appliserver.com>
Newsgroups comp.lang.javascript
Subject Re: Create cookie from ID of <a> tag
Date 2021-03-27 12:08 +0100
Organization A noiseless patient Spider
Message-ID <s3n3nm$760$1@dont-email.me> (permalink)
References (3 earlier) <s3hk8a$g7n$1@dont-email.me> <48e39d8c-ec2d-4759-8ddd-8ac063e510c3n@googlegroups.com> <cca12429-db8b-4747-990f-0f9e2fedba5dn@googlegroups.com> <s3k5e5$aot$1@dont-email.me> <1931004.oMNUckLgyt@PointedEars.de>

Show all headers | View raw


Il 26/03/2021 17:31, Thomas 'PointedEars' Lahn ha scritto:
> Daniele wrote:
> ^^^^^^^
> Please post using your real name (first name and last name suffice).
> 
Gosh, I chaged my ID string to meet your suggestions some time (years?) ago.
It seems that it is not enough, anyway. :-)

>> Hi, this is the code I use to manage cookies:
>>
>> function createCookie(name,value,days) {
>> if (days) {
>> var date = new Date();
>> date.setTime(date.getTime()+(days*24*60*60*1000));
> 
> As we know the number of (milli)seconds per standard day,
> 
>    … + days * 864e5
> 
> would be more efficient (if this way of moving a date value forward one day
> would be safe).
> 
>> function readCookie(name) {
>>    var nameEQ = name + "=";
>>    var ca = document.cookie.split(';');
>>    for(var i=0;i < ca.length;i++) {
> 
>      [“for” is not a function.  Do not write it as if it were.]
> 
>      for (var i = 0, len = ca.length; i < len; ++i) {
> 
>>      var c = ca[i];
>>      while (c.charAt(0)==' ') c = c.substring(1,c.length);
> 
> Ouch.
> 
>    c = c.replace(/^ +/, '');
> 
>>      if (c.indexOf(nameEQ) == 0) return
> c.substring(nameEQ.length,c.length);
> 
> The argument c.length is superfluous.
> 
>>    }
>>    return null;
> 
>   The return value on failure should be “undefined” instead because the
> function returns a primitive (String) value, not an object value, if
> successful.
> 
>> }
> 
> That is *very* inefficient and error-prone.  Consider this:
> 
>    function readCookie (name)
>    {
>      return (
>        document.cookie.split(';')
>          .ca.map((cookie) => cookie.split('='))
>          .filter((cookie) => cookie[0] == name) || []
>      )[0];
>    }
> 
>      [without .filter(), this is useful as an approach to obtain *all*
>       cookies in an easily processible data structure],
> 
> or
> 
>    function readCookie (name)
>    {
>      /*
>       * NOTE: Cookie names typically do not contain
>       * RegExp special characters; otherwise those
>       * need to be escaped.
>       */
>      return (document.cookie
>        .match(new RegExp('(?:^|;)' + name + '=([^;]+)')) || [])[1];
>    }
>    
> 
wow. Thank you Thomas.
Unfortunately, this use of "=>" is new to me.
I'll dig into it.

Dan

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