madmurphy / cookies.js

Simple cookie framework with full Unicode support
GNU General Public License v3.0
265 stars 54 forks source link

Does this still work? #2

Closed konsumer closed 7 years ago

konsumer commented 7 years ago

I tried it out on this codepen and it doesn't appear to persist. Does it still work? Am I using it wrong?

const count = docCookies.getItem('count') || 0
document.body.innerHTML = count
docCookies.setItem('count', count + 1) 
madmurphy commented 7 years ago

@konsumer

With Firefox your codepen kind of works – the only problem is that you have to parseFloat() the old value if you want to sum anything to it:


const count = docCookies.getItem("count") || "0";
document.body.innerHTML = count;
docCookies.setItem("count", parseFloat(count) + 1);

Moreover, without setting a duration, you are telling the browser that the cookie is a “session cookie”. In this case the duration of the cookie will be browser-dependent (and this framework has no control on that).

Last but not least: The keyword const is not supported in many browsers – use var instead.

konsumer commented 7 years ago

Hmm. Ok, so in Chrome 56.0.2924.87 const is supported and the pen still doesn't work, but I get no console errors.

If the problem was using a string of a number was making it fail, I should get "11" when I add "1" to "1".

I made the changes you suggested anyway, and it still isn't working for me. I originally made this as a demonstration of cookies used in place of localStorage, and hoped it work very similar, but it seems to not.

Here is the new code:

var count = docCookies.getItem("count") || "0"
document.body.innerHTML = count
docCookies.setItem("count", parseFloat(count) + 1, new Date('12/01/2020'))

Here is the cookie (with an expiration), in dev-tools:

screen shot 2017-04-01 at 1 32 06 pm

if I refresh, the cookie value remains 1, and the inserted text is "0"

konsumer commented 7 years ago

I also did more type-related stuff here, just to make sure it was the type expected at every access, and not being silently coerced into something I didn't expect. Again, no error, and the first cookie is set, but my code can't seem to read the value:

var count = parseInt(docCookies.getItem("count"))
document.body.innerHTML = count.toString()
docCookies.setItem("count", (count + 1).toString(), new Date('12/01/2020'))

I also verified these situations, to ensure that the var count line is working as I expect (all return true):

0 === (parseInt(undefined) || 0)
0 === (parseInt("") || 0)
0 === (parseInt(NaN) || 0)
0 === (parseInt("NaN") || 0)
screen shot 2017-04-01 at 1 49 29 pm
madmurphy commented 7 years ago

@konsumer I opened your new codepen with Android's Browser and it works.

EDIT: Tried also with GNOME's Epiphany (WebKit-based) and Firefox. Seems to work fine as well.

konsumer commented 7 years ago

Yeh, works fine in Firefox mobile & chrome mobile. So strange that it doesn't work in Chrome desktop, for me.

konsumer commented 7 years ago

Also, as a sidenote, this one works fine in Chrome (example).

madmurphy commented 7 years ago

Yes, it's strange. I think that that script automatically uses "/" as path when this is not explicitly given. Maybe you could you try to replace the third line of your code with


docCookies.setItem("count", (count + 1).toString(), Infinity, "/")
konsumer commented 7 years ago

Yep, that works! Thanks.

madmurphy commented 7 years ago

You are welcome :)

Just one more thing. Since I cannot think that Google Chrome suddenly got crazy, I suspect that this problem has more to do with the way codepen manages its pages instead. Could you please try to restore the old third line and put it on another website as a static page? I would try this previous version:


const count = docCookies.getItem("count") || "0";
document.body.innerHTML = count;
docCookies.setItem("count", parseFloat(count) + 1);

Let me know!

konsumer commented 7 years ago

I also suspect something might just be funny with codepen. Earlier version works fine on local static webserver, but silently fails with file access.

<body style="font: 100px sans-serif;"></body>
<script src="https://rawgit.com/madmurphy/cookies.js/master/cookies.js"></script>
<script>
const count = docCookies.getItem("count") || "0";
document.body.innerHTML = count;
docCookies.setItem("count", parseFloat(count) + 1);
</script>
madmurphy commented 7 years ago

As I suspected. Thanks for the test!

but silently fails with file access

If I don't remember wrong Chrome doesn't accept cookies locally, neither natively nor with any cookie library.