• Accessing the value of a cookie

    From groovee@groovee@cyberdude.com to comp.lang.javascript on Tue Nov 20 23:33:51 2018
    From Newsgroup: comp.lang.javascript

    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?
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From JJ@jj4public@vfemail.net to comp.lang.javascript on Wed Nov 21 16:03:00 2018
    From Newsgroup: comp.lang.javascript

    On Tue, 20 Nov 2018 23:33:51 -0800 (PST), groovee@cyberdude.com wrote:
    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?

    See:

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

    Hint: bookmark that site and use it as JavaScript reference. It'll save you
    so much time.
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Ben Bacarisse@ben.usenet@bsb.me.uk to comp.lang.javascript on Wed Nov 21 12:37:48 2018
    From Newsgroup: comp.lang.javascript

    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.
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From groovee@groovee@cyberdude.com to comp.lang.javascript on Thu Nov 22 00:56:40 2018
    From Newsgroup: comp.lang.javascript

    On Wednesday, 21 November 2018 18:07:55 UTC+5:30, Ben Bacarisse wrote:

    The original code (which you altered) is intended to find out if a key exists, not what value it has.

    I did not! This page, look at Example 6 (I have a feeling you were looking at 5!):

    https://developer.mozilla.org/en-US/docs/Web/API/document/cookie


    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Ben Bacarisse@ben.usenet@bsb.me.uk to comp.lang.javascript on Thu Nov 22 12:22:24 2018
    From Newsgroup: comp.lang.javascript

    groovee@cyberdude.com writes:

    On Wednesday, 21 November 2018 18:07:55 UTC+5:30, Ben Bacarisse wrote:

    The original code (which you altered) is intended to find out if a key
    exists, not what value it has.

    I did not! This page, look at Example 6 (I have a feeling you were
    looking at 5!):

    Yes, you are right -- I was looking at the very similar previous
    example. Sorry about that.

    The code shown in both exercises does not do what it purports to do.
    Don't copy it. It did not occur to me that such a bad example would be
    given on the MDN website and I should therefore also apologise for
    assuming you'd written anything that bad!

    https://developer.mozilla.org/en-US/docs/Web/API/document/cookie
    --
    Ben.
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Julio Di Egidio@julio@diegidio.name to comp.lang.javascript on Sat Nov 24 06:11:41 2018
    From Newsgroup: comp.lang.javascript

    On Wednesday, 21 November 2018 08:34:01 UTC+1, gro...@cyberdude.com wrote:

    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:

    The code in those examples (and in technical examples in general) is *not* meant to be *production-ready*.

    For your benefit, follows an example of the code I am using in production:
    it is not totally abstract nor particularly sophisticated, but I'd think at least the cookie handling logic is correct to the standard:

    --------------------------------------------------------------
    function $SettingNeverExpires()
    {
    // virtually unlimited
    var expires = new Date();
    expires.setFullYear(expires.getFullYear() + 1);
    return expires;
    }

    function $loadSettings(itemCb) // calls itemCb per each name-value
    {
    var cookies = $doc.cookie.split(";");

    for (var i = 0; i < cookies.length; ++i)
    {
    var pair = cookies[i].split("=");

    if (pair.length === 2)
    {
    var name = decodeURIComponent(pair[0]).trim();
    var value = decodeURIComponent(pair[1]).trim();

    itemCb(name, value);
    }
    }
    }

    function $saveSettings(dict) // dict is simply an object
    {
    var exp = $SettingNeverExpires().toUTCString();

    for (var key in dict)
    {
    var name = encodeURIComponent(key);
    var value = encodeURIComponent(dict[key]);

    $doc.cookie = name + "=" + value + "; expires=" + exp;
    }
    }
    --------------------------------------------------------------

    Feel free to peruse. And, everybody, please feel free to nitpick...

    Thanks,

    Julio
    --- Synchronet 3.20a-Linux NewsLink 1.114