Closed konsumer closed 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.
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:
if I refresh, the cookie value remains 1, and the inserted text is "0"
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)
@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.
Yeh, works fine in Firefox mobile & chrome mobile. So strange that it doesn't work in Chrome desktop, for me.
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, "/")
Yep, that works! Thanks.
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!
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>
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.
I tried it out on this codepen and it doesn't appear to persist. Does it still work? Am I using it wrong?