News for hackers. Stuff that matters.
Re:Gov.uk drops jQuery from their front end by spicybright on Thursday May 19, 2022 @09:57AM (#31434721)
Eh. Everyone hates on jQuery, but for most informational sites that's really all you need for basic interaction.

Speed really isn't bad, and it's so straightforward that it's hard to mis-use at this point.

I seem to have way more issues with newer frameworks/SPA if I'm not using chrome with no extensions.

Makes me nervous dealing with important stuff like banking or government sites.

Re:Gov.uk drops jQuery from their front end by jaywalk on Thursday May 19, 2022 @10:00AM (#31434760)
For basic interaction, you don't need anything but vanilla JS. Browsers have come a long way since the days where jQuery was almost necessary to deal with all of the minor differences/bugs in various browsers.
Re:Gov.uk drops jQuery from their front end by spicybright on Thursday May 19, 2022 @10:09AM (#31434855)
I'll admit I'm a bit out of practice with the state of vanilla JS, but it seems like you'll be re-inventing the wheel for simple stuff.

Like a collapsible section that's lightly animated.

I worry too about people not considering accessibility.

A lot of my bugs day to day is not being able to tab into things like hover menus or being able to activate buttons because someone forgot to consider correct event callbacks.

Maybe it's different now, but using a jQuery makes it more likely the code will cover cases like that.

Re:Gov.uk drops jQuery from their front end by onion2k on Thursday May 19, 2022 @10:26AM (#31435076)
I worry too about people not considering accessibility.

This isn't a complaint you can level at the gov.uk team. They're obsessed with accessibility. Gov.uk is probably the best example of a well-designed, well-built, completely accessible website on the whole internet.

Re:Gov.uk drops jQuery from their front end by spicybright on Thursday May 19, 2022 @10:33AM (#31435180)
I meant more for anyone building a website.
Re:Gov.uk drops jQuery from their front end by psnehanshu on Thursday May 19, 2022 @04:55PM (#31440044)
Whilst *.GOV.IN (India) is the worst.
Re:Gov.uk drops jQuery from their front end by chipsa on Thursday May 19, 2022 @10:23AM (#31435040)
Collapsible section? https://developer.mozilla.org/en-US/docs/Web/HTML/Element/de...

There's some CSS transition stuff you can add to that to make it lightly animated: https://css-tricks.com/how-to-animate-the-details-element/

Yeah, jQuery might make some things easier, but when possible, using native HTML elements makes things even easier,

Re:Gov.uk drops jQuery from their front end by nhinck on Thursday May 19, 2022 @10:50AM (#31435406)
Hmm, I would agree that you can do 95%+ of stuff jQuery used to be used for in vanilla js or html/css in a nicer way but animated collapsible divs still require a stupid amount of js to achieve.
Re:Gov.uk drops jQuery from their front end by iCarrot on Thursday May 19, 2022 @11:31AM (#31436011)
Animated collapsible div can be done with 0 line of JS.

https://code-boxx.com/expand-collapse-div-css-only/

Re:Gov.uk drops jQuery from their front end by nhinck on Friday May 20, 2022 @07:02AM (#31445722)
Uhh, did you check out how that actually functions? It looks like garbage.
Re:Gov.uk drops jQuery from their front end by ThunderSizzle on Friday May 20, 2022 @11:04AM (#31448444)
Agreed, this example looks like total shit.
Re:Gov.uk drops jQuery from their front end by Cthulhu_ on Thursday May 19, 2022 @10:23AM (#31435037)
In 2020, a collapsible section is a matter of adding / removing a classname and a CSS animation; there's a few ways to do so, using jQuery is one, but you don't need thousands of lines of a JS library (network bandwidth + JS interpretation) to achieve that.
Re:Gov.uk drops jQuery from their front end by have_faith on Thursday May 19, 2022 @10:52AM (#31435432)
Can CSS transitions and animations handle animating to auto height/widths yet?
Re:Gov.uk drops jQuery from their front end by d1sxeyes on Thursday May 19, 2022 @11:31AM (#31435997)
I don't think so. One trick I've used to good effect is setting an high max-height, then translating by 100% and removing pointer events.
Re:Gov.uk drops jQuery from their front end by flatcakes on Thursday May 19, 2022 @11:37AM (#31436095)
I feel like this solution feels subtly "wrong" if you're animating, because that high max height will be arbitrarily far away from where you'd normally want to animate to. Doubly so for easing out.
Re:Gov.uk drops jQuery from their front end by d1sxeyes on Thursday May 19, 2022 @12:00PM (#31436361)
Yeah it's a bit weird, but haven't found a better trick yet.
Re:Gov.uk drops jQuery from their front end by pacaro on Thursday May 19, 2022 @10:14AM (#31434921)
You bring up some interesting concerns. It's a good example of why there isn't a one size fits all answer. The UK gov site has high standards for these kind of things so will do it right anyway, but for a small shop leaning on a framework makes a lot of sense
Re:Gov.uk drops jQuery from their front end by mitchdoogle on Thursday May 19, 2022 @10:39AM (#31435256)
If you've been using jQuery for years, know it, and can write a good amount of code without looking at a reference, then by all means, keep using it. It's not going anywhere anytime soon.
Re:Gov.uk drops jQuery from their front end by andybak on Thursday May 19, 2022 @10:06AM (#31434822)
It's a much nicer syntax and has a ton of useful helpers.

Vanilla js for similar things just looks like someone has waved the ugly stick at it.

There are sound arguments against jQuery but the "you can just use vanilla" is the least convincing.

Re:Gov.uk drops jQuery from their front end by jaywalk on Thursday May 19, 2022 @10:09AM (#31434858)
Not having to load yet another JS library on your page if you don't really need it is a pretty good argument, I'd say.
Re:Gov.uk drops jQuery from their front end by kijin on Thursday May 19, 2022 @10:17AM (#31434967)
Except we've become so used to loading multiple megabytes of JS even on a simple blog post, that a 30KB library probably won't make a noticeable difference. Insofar as file size is concerned, I'd much rather pull in jQuery than some random npm package that depends on god-knows-how-many other random packages.

Even GOV.UK, an incredibly lean website by today's standards, only noticed a 10% difference in their benchmarks.

Re:Gov.uk drops jQuery from their front end by Deukhoofd on Thursday May 19, 2022 @10:20AM (#31435006)
A 10% performance gain is quite a big deal, I wouldn't just shove it under the table like that.
Re:Gov.uk drops jQuery from their front end by Dudeman112 on Thursday May 19, 2022 @12:46PM (#31436973)
Like every % comparison, it depends on the baseline.

Cue all the people going on about big O for parts of the code that are indistinguishable from noise in actual benchmarks.

Re:Gov.uk drops jQuery from their front end by andybak on Thursday May 19, 2022 @10:23AM (#31435044)
Yes. You'll note that I already anticipated that in my original point. I was very specific in how I worded it for good reason.
Re:Gov.uk drops jQuery from their front end by mejutoco on Thursday May 19, 2022 @12:04PM (#31436424)
This is not the case anymore but I remember when Angular was including a version of jQuery, and having methods like "doSthAsInJquery" (paraphrasing) in Angular, and I could still hear everyday that Angular didn't have the overhead of including jQuery.

I wish we could have standardized in the Linq-like API of jQuery.

Re:Gov.uk drops jQuery from their front end by sofixa on Thursday May 19, 2022 @10:16AM (#31434952)
OP's argument is that jQuery is all you need for basic use cases, so that'd be the only JS library loaded.
Re:Gov.uk drops jQuery from their front end by dgb23 on Thursday May 19, 2022 @10:29AM (#31435129)
To add: jQuery is a pretty huge library for what it does. Talking both bundle size and execution speed. Not that the latter is noticeable for most cases, but it's still a cost. The question is: What for?

The primary use case for it has been cross browser compatibility plus a bit of sugar. The former is _gone_ and the latter is easily replaced with a few lines of JS. Then there is an ecosystem of libraries and components around it. Many of them have ditched jQuery or have provided an API that doesn't require it.

Re:Gov.uk drops jQuery from their front end by rtpg on Thursday May 19, 2022 @10:34AM (#31435195)
30kb minified and gzipped! Less than most hero images. It's a nice API for simple stuff.

I think if you strip out the AJAX support it can get extremely tiny.

Re:Gov.uk drops jQuery from their front end by dgb23 on Thursday May 19, 2022 @12:46PM (#31436971)
It adds tens of milliseconds to just downloading on a fast connection, plus it has to be parsed before what ever you need it to do will be functional.

To compare, just roughly:

For roughly the same size you can get declarative/reactive rendering with React, or an optimized FP standard library with ClojureScript, or data driven visualizations with d3 (roughly 2x the size).

For a tenth of the size you can get stuff like htmx, or a json validation library (avj), or like someone said Preact (React alternative).

But for jQuery, you're paying that for basically nothing the browser APIs don't already provide. It was a godsend 10y ago, but with legacy browsers finally fading out, the last one being ie11, you really don't _need_ it anymore.

Re:Gov.uk drops jQuery from their front end by Rumudiez on Thursday May 19, 2022 @10:41AM (#31435280)
30kb gzipped is still 10x larger than Preact for reference
Re:Gov.uk drops jQuery from their front end by SemanticStrengh on Thursday May 19, 2022 @11:12AM (#31435747)
that doesn't make it an issue in any way, it's not a numbers competition unless you have a 56K
Re:Gov.uk drops jQuery from their front end by Smaug123 on Thursday May 19, 2022 @02:34PM (#31438361)
One of the explicit goals of gov.uk is to be maximally accessible, and that includes for people accessing the Internet on a potato or who have a metered connection.
Re:Gov.uk drops jQuery from their front end by lowwave on Thursday May 19, 2022 @10:53AM (#31435448)
or use https://axios-http.com/ for ajax stuff
Re:Gov.uk drops jQuery from their front end by jenscow on Thursday May 19, 2022 @10:55AM (#31435469)
Right. However, jQuery isn't just for cross-browser.

It provides a clean API for manipulating the DOM, as this site demonstrates (which is ironic): https://youmightnotneedjquery.com/

Personally, for simple sites, I aim for vanilla JS but use jQuery (cash, actually) where it makes sense - such as working on collections of elements.

Re:Gov.uk drops jQuery from their front end by SemanticStrengh on Thursday May 19, 2022 @11:10AM (#31435720)
for real its superior ergonomy is painfully obvious
Re:Gov.uk drops jQuery from their front end by criddell on Thursday May 19, 2022 @11:15AM (#31435776)
You can do quite a bit without Javascript too. A lot of websites don't need much more than simple menus.

Steve Gibson's site is an example:

https://www.grc.com/default.htm

Re:Gov.uk drops jQuery from their front end by seydor on Thursday May 19, 2022 @10:49AM (#31435393)
i see a lot of

   const $ = document.querySelector.bind(document);
Re:Gov.uk drops jQuery from their front end by phkahler on Thursday May 19, 2022 @10:31AM (#31435157)
>> Eh. Everyone hates on jQuery, but for most informational sites that's really all you need for basic interaction.

The point seems to be that you don't even need that any more, so just get rid of it. Not sure why people latch onto cruft so much and defend its continued use.

On the performance side they cite 10 percent improvement, which doesn't sound like much to a lot of people. But remember that performance gains/regressions get compounded. Maybe there are 8 things that each eat 10 percent - you either start improving somewhere or you just refuse to even try. If I could improve 10 percent while removing a dependency I'd be all over that.

Re:Gov.uk drops jQuery from their front end by cies on Thursday May 19, 2022 @12:14PM (#31436555)
> Not sure why people latch onto cruft so much and defend its continued use.

A characteristic of communities with lots of people that do not have a theoretical background. JS, PHP, jQuery, some parts of Ruby/Python -- they defend it with their life, and I argue it's because the rest of the world looks very scary to them.

Re:Gov.uk drops jQuery from their front end by AdrianB1 on Thursday May 19, 2022 @10:35AM (#31435206)
For AJAX, jQuery is still simpler than vanilla JS.
Re:Gov.uk drops jQuery from their front end by barbecue_sauce on Thursday May 19, 2022 @10:43AM (#31435321)
Even fetch?
Re:Gov.uk drops jQuery from their front end by moreira on Thursday May 19, 2022 @10:57AM (#31435509)
Definitely not Fetch. I don't believe anyone can argue that Fetch is more complicated.

I think this really seems to stem from a "I'm only used to jQuery, anything else is a big mental effort to learn and I'd rather stick with what I know".

I get it. Vanilla JS, if you're not used to it, is scary. But if you're advocating for jQuery "for simple interactions", and assuming it's simpler than vanilla JS, at some point you should ask yourself if you're just letting your skills stagnate, and staying stuck in the past. You owe it to yourself to update your skillset. If you decide you still need jQuery (e.g. maybe you need a plugin/library that requires jQuery), then fine. But at least you'll be making an informed decision, not a FUD-driven one.

Re:Gov.uk drops jQuery from their front end by easrng on Thursday May 19, 2022 @12:25PM (#31436694)
I mean `await $.ajax("https://jquery.com/")` is shorter than `await(await fetch("https://jquery.com/")).text()`
Re:Gov.uk drops jQuery from their front end by epolanski on Thursday May 19, 2022 @12:02PM (#31436399)
As someone kinda obsessed with handcrafting all of its software without any dependency and is highly used to dom operations, I can't but thing you're making it much more of a deal than it is.

Handcrafting JavaScript applications without jQuery is possible, the real question though is: is it convenient if you like and find productive using jQuery? Here's a fact: you can always remove jQuery later when the product is mature and standards understood and there is time budget for it.

Re:Gov.uk drops jQuery from their front end by philliphaydon on Thursday May 19, 2022 @10:05AM (#31434813)
I loved jquery back in the day. It made web development bearable in a time when it was a clusterfuck.

I think a lot of people owe a lot of thanks to jquery.

But in 2022 vanilla javascript is all you really need.

Re:Gov.uk drops jQuery from their front end by dsego on Thursday May 19, 2022 @10:10AM (#31434873)
jQuery still offers a much nicer experience. The built-in dom apis are neither very elegant nor ergonomic.

    // jquery
    $(selector).on('click', fn)
     
    // vanilla js
    document.querySelectorAll(selector).forEach(el => 
      el.addEventListener('click', fn)
    )
Re:Gov.uk drops jQuery from their front end by bennyp101 on Thursday May 19, 2022 @10:18AM (#31434973)
It takes 1min to write a wrapper around that, though.

Having said that, for the sake of 25kb - if you use more than a handful of features you might as well just include it.

Re:Gov.uk drops jQuery from their front end by mkoryak on Thursday May 19, 2022 @10:27AM (#31435097)
That's the problem.

It takes only 1 minute to write a wrapper, so you will write one, and I'll write one, and your dog will write one.

Re:Gov.uk drops jQuery from their front end by JJMcJ on Thursday May 19, 2022 @01:34PM (#31437663)
If that is literally the only wrapper you need, sure go ahead.

But most of the time, you find you need another wrapper, and then another, and the next thing you know you have most of jQuery, just not as uniform or as well tested.

I've seen this with database ORMs also.

Re:Gov.uk drops jQuery from their front end by tiborsaas on Thursday May 19, 2022 @11:18AM (#31435833)
Then compare it with dog's and pick the simpler one :) I don't know what's wrong with a utils.js file that only has what you need in a way that fits your project exactly. Woof.
Re:Gov.uk drops jQuery from their front end by mkoryak on Friday May 20, 2022 @05:02PM (#31452462)
your dog will not look for a simpler impl before making one.

your npm's dependencies authors cat will write a small wrapper and wont know about yours.

Re:Gov.uk drops jQuery from their front end by DeathArrow on Thursday May 19, 2022 @02:41PM (#31438448)
But writing that utils.js will end up rewriting jQuery.
Re:Gov.uk drops jQuery from their front end by tiborsaas on Thursday May 19, 2022 @06:00PM (#31440704)
I've seen this argument a lot, but based on my experience it will take ~5 small functions to get what I need and how I need.

It's even easier if you copy paste from here: https://anguscroll.com/just/

Re:Gov.uk drops jQuery from their front end by Cthulhu_ on Thursday May 19, 2022 @10:25AM (#31435073)
Is it worth adding the library (download size + script parsing & execution) for that though?

At the very least there are some jQuery alternatives that are modular, smaller, and have less backwards compatibility like iirc zepto.

Re:Gov.uk drops jQuery from their front end by dageshi on Thursday May 19, 2022 @10:53AM (#31435445)
Yup. Because the size of the library and the execution time just isn't that big of a deal, it's something people seem to obsess over but it doesn't really matter much.
Re:Gov.uk drops jQuery from their front end by louissan on Thursday May 19, 2022 @10:25AM (#31435068)
or delegate...?

document.addEventListener('click', function(e){ var t = e.target;

  if(t.closest('.foo') && t.nodeName.toLowerCase() === 'button'){
     e.preventDefault();
     // do stuff here
  }
  else if(t.closest('.bar') && t.nodeName.toLowerCase() === 'a' && t.classList.contains('here')){
     e.preventDefault();
     // do smth else here
  }
}, false);

one listener to rule them all...?

elegant enough and once you get used to it...

.closest can be pollyfilled too..

Re:Gov.uk drops jQuery from their front end by fleddr on Thursday May 19, 2022 @10:23AM (#31435049)
Fully agree, and your example is still relatively simple. Method chaining in jQuery allows for extremely expressive one-liners that would require a huge amount of boilerplate in vanilla JS.
Re:Gov.uk drops jQuery from their front end by muhehe on Thursday May 19, 2022 @10:19AM (#31434989)
Web development is still a clusterfuck.
Re:Gov.uk drops jQuery from their front end by DeathArrow on Thursday May 19, 2022 @02:43PM (#31438465)
I agree if you are talking about frontend. Backend is still pleasant. And even frontend becomes bearable thanks to HTMX.
Re:Gov.uk drops jQuery from their front end by nojs on Thursday May 19, 2022 @10:13AM (#31434904)
If you just want “basic interaction” without any framework complexity, alpine.js is way better than jquery. It’s extremely simple and does the basic data binding stuff that jquery lacks.
Re:Gov.uk drops jQuery from their front end by duxup on Thursday May 19, 2022 @10:25AM (#31435067)
I will always think fondly of jQuery. It did a great job for what it was intended, saved a lot of time. jQuery's time has passed, and that's ok too.