3 ms·
I'm sorry, I really hate to be condescending. I understand there are a range of levels of JS expertise out there, and using either jQuery or an appropriate libr
by lucideer 10y ago
I'm sorry, I really hate to be condescending. I understand there are a range of levels of JS expertise out there, and using either jQuery or an appropriate library represents real value for many.
However, when you're opening a comment with "Hell yeah, you do", I really have to respond.
Hell no, you don't. If you're writing 318 lines of code just to do a cookie warning, that's fine. But please don't preach that it's necessary before actually learning some javascript.
I'm also a bit baffled that it would take an entire 30 lines while using jQuery AND a cookie plugin. What are you doing to these cookies messages???
::EDIT::
I have no idea if the code below works, and it could do with a lot of improvement (thinking about secure cookies, cookie paths, or even just using localStorage instead), but I can't imagine a bit of testing and revision of the code would balloon it to even twice its size. Least of all 30 times its size.
(function() {
var cookiesMessage = document.getElementById('cookies-message-wrap');
var cookiesAccept = document.getElementById('cookies-accept-button');
var checkCookies = function () {
cookiesMessage.setAttribute('class', document.cookie.indexOf('cookiesaccepted=1')===-1?'':'accepted');
}
cookiesAccept .addEventListener('click', function() {
document.cookie = 'cookiesaccepted=1;expires=Tue, 19 Jan 2038 03:14:07 GMT;path=/';
checkCookies();
});
})();
What am I missing?
- throwanem 10y agoI'd default the notice invisible and show it when it hasn't been accepted, rather than vice versa as you do here. That way, in the more common case, nothing needs doing. (Maybe there's a regulatory reason that won't fly? I don't know or care much beyond the obvious about the EU cookie rule.) I'd also break out some of the inlining, just to make it read more easily, if this were destined for production. But the functionality looks good! I mean, document.cookie can be a huge pain to deal with if you actually have to deal with it, and jQuery has a plugin for that - but this doesn't take any kind of parsing, just a cheap substring check, and the class list manipulation is equally trivial. Even animation could be handled in CSS. Perhaps OP's use case is much more complex than it sounds, in a way that's far from obvious?
- lucideer 10y agoI'm not sure what the regulation would say, but if I'm choosing to comply, I'm going to default to visible as absence of JS doesn't equal absence of cookies. It maybe annoying that the accept button won't work, but at least the user is informed, which I guess was the intent of the (tbh misguided) regulation.