4 ms·
Hell yeah, you do. I just recently had to create a tiny script (one of those notorious cookie warnings) without any dependencies. It's like pulling teeth. The D
by dr_faustus 10y ago
Hell yeah, you do. I just recently had to create a tiny script (one of those notorious cookie warnings) without any dependencies. It's like pulling teeth. The DOM API is an ugly, verbose mess and its pretty unbareble to use without JQuery, zepto, etc.. Including the code for parsing cookies (yeah, you still have to do that, too) the script clocked in at 318 lines of code (without comments).
With JQuery and the cookies plugin its would probably have been something like 30 lines. Thats 10 times the code you have to write, debug and maintain.
So, of course, you don't NEED JQuery just like you don't NEED any means of transportation, you can walk everywhere after all. The question is, if you want to spend you whole life walking/writing verbose ugly JS code.
- akx 10y agoCurious: why'd you feel the need to write your own? https://cookieconsent.insites.com/demos/ https://cookieconsent.insites.com/demos/ is nice and light.
- nautical 10y agoInteresting .. https://github.com/js-cookie/js-cookie/blob/master/src/js.cookie.js https://github.com/js-cookie/js-cookie/blob/master/src/js.co... is under 165 lines with comments and provides a nice API on top .
- franciscop 10y agoYou also got my own cookies library https://github.com/franciscop/cookies.js https://github.com/franciscop/cookies.js at 107 lines (97 LoC), with the minified+gzipped version being 846 bytes. It can be easily copy/pasted into your code if you don't want the overhead of an extra request, or just using unpkg CDN: <script src="https://unpkg.com/cookiesjs@1"></script>
- lucideer 10y agoI'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.
- GoAheadDoIt 10y agodr_faustus needs Jquery :)
- Cozumel 10y agodr_faustus needs to learn actual Javascript before embarrassing himself here :)
- austincheney 10y agoAs a long time JavaScript developer jQuery feels like welfare. You can absolutely live without it. Living without it means you will find ways to perform better. For people who have never lived without it the world is a scary place. God forbid anybody suggest an alternative to an entitlement.
- lucideer 10y agoI'd agree with the initial intent of your comment (as I'm sure many here may), but your analogy I find deeply, deeply offensive.
- austincheney 10y agoWhy? I think if you are offended it makes the analogy even more precise.
- lucideer 10y ago- Some absolutely cannot live without it. - "finding ways to perform better" by living without it is not something that happens in real life. To the contrary, living without adequate financial support has been shown by many studies to lead to anxiety, mental issues and general under-performance. - It's clearly an unnecessary and disingenuous attempt to bring a loaded political agenda of your own into an otherwise innocuous discussion of programming libraries.
- austincheney 10y agoAgain, I believe your opinions on this matter absolutely reinforce the analogy. I can say this with clarity and objectivity because I have had to rely on government handouts a long time ago when I had my first kiddo while being an unemployed college student only to drop out of school and work below the poverty line. Some people can figure it out and others cannot. The embarrassment and offense are very much a part of that issue as well as the imposter syndrome that is so obvious in so many developers.