Live data from Hacker News

Gov.uk drops jQuery from their front end

web.dev

101–110 of 458 posts

Re: Gov.uk drops jQuery from their front end

#101
post #71
post #38

Javascript has become so nice, now that browsers support modules. I use a module "dqs.js" for all my query needs. It contains only these two lines: export const dqs = document.querySelector.bind(document); export const dqsA = document.querySelectorAll.bind(document); And use it like this: import { dqsA } from 'lib/dqs.js'; for (const rabbit of dqsA('#rabbits .green')) { ... do something with all green rabbits ... }

There is .forEach() on NodeLists, so you can do stuff like this in every browser, no lib, no helper functions needed: document.querySelectorAll('a').forEach(tag => { // operate on tag })

alternatively, if you need to use anything other than `forEach` for working on the list, you can turn the NodeList into an array using the array spread syntax (equivalent to using Array.from) e.g.

  [...document.querySelectorAll('a')]
  .filter(node => node.getAttribute("data-foo") === "bar")

Re: Gov.uk drops jQuery from their front end

#102

Earlier quoted context omitted.

By "more efficient" it seems like you mean "less typing" because that is significantly less efficient in terms of downloaded code size and performance (the jQuery code calls the vanilla code.) PS. $ is an alias for document.getElementById so you can just do $('my_id').style.display = 'none'.

most people probably have jquery cached in their browser if you're pulling it from the google cdn

Thanks to cache partitioning, this is no longer true.

See, it turns out that by carefully leveraging cached retrieval of third party resources, scripts on two different domains can figure out if they are running in the same web browser, effectively pulling off browser fingerprinting.

To stop this, modern browsers don’t share third party resource caches cross domain.

So using a well known CDN no longer confers any benefit - even if a user has pulled down jquery for another site’s benefit, when they come to your site and you request the same resource, the browser will go out and re-retrieve it, to prevent you or the third party domain from being able to infer anything about the user based on the speed of the response or whether or not the request got made.

Re: Gov.uk drops jQuery from their front end

#103
post #27

Tangential, but sometimes I see people say that jQuery is dead/failed. While it's not as popular anymore, I think that's actually the perfect evolution for it. jQuery was making up for browser API shortcomings, and now many of those shortcomings have been addressed and been incorporated into browser APIs (not everything, but quite a bit).

> jQuery was making up for browser API shortcomings

While this is true, I can still code circles around frontend developers using vanilla JS.

The decline of jQuery is mostly due to the rise of webapp frameworks like React and Vue. But if you are tasked with adding like, a button to a static webpage, jQuery is still the king.

Re: Gov.uk drops jQuery from their front end

#104

Earlier quoted context omitted.

Can CSS transitions and animations handle animating to auto height/widths yet?

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.

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

#105

I still use jQuery for frontend JavaScript when I can. It's so much more efficient than native JavaScript. I can't imagine anyone prefers to write document.getElementById('element').style.display = 'none' rather than $('#element').hide();

jQuery was great in its time and still has some places where it can be a good tool for the job, but it isn't used less now because people like writing the longer form of Javascript; it's used less now because for many projects people are using frontend frameworks that let you operate at a higher level altogether.

Re: Gov.uk drops jQuery from their front end

#106

I still use jQuery for frontend JavaScript when I can. It's so much more efficient than native JavaScript. I can't imagine anyone prefers to write document.getElementById('element').style.display = 'none' rather than $('#element').hide();

Does your IDE autocomplete the jQuery code though? I'd imagine it wouldn't. But it would for the native code.

Re: Gov.uk drops jQuery from their front end

#107

Earlier quoted context omitted.

By "more efficient" it seems like you mean "less typing" because that is significantly less efficient in terms of downloaded code size and performance (the jQuery code calls the vanilla code.) PS. $ is an alias for document.getElementById so you can just do $('my_id').style.display = 'none'.

most people probably have jquery cached in their browser if you're pulling it from the google cdn

As others have mentioned this isn't the case, also the browser still has to parse that JavaScript, which for a lower end mobile phone is often significant.

Re: Gov.uk drops jQuery from their front end

#108

Earlier quoted context omitted.

most people probably have jquery cached in their browser if you're pulling it from the google cdn

Thanks to cache partitioning, this is no longer true. See, it turns out that by carefully leveraging cached retrieval of third party resources, scripts on two different domains can figure out if they are running in the same web browser, effectively pulling off browser fingerprinting. To stop this, modern browsers don’t share third party resource caches cross domain. So using a well known CDN no longer confers any ben…

ahh wasn't aware, and good to know

Re: Gov.uk drops jQuery from their front end

#109
post #18

The gains aren't massive — certainly not something any human using the site would notice (about 20ms faster to render pages for the average mobile phone user). Still good to remove unncessary dependencies, though.

Not on an individual scale, no, but when you consider the millions of visits and views they get per year it adds up from a global / zoomed out point of view. I'm sure with some math you can make a calculation on how much bandwidth and energy was saved with this One Clever Trick.

A government is the perfect place to look at this "zoomed out" view.

Your citizens an average rate of pay of Y per year. Some of time they save by getting stuff done on a government website will be put into getting more economic work done. If the citizens do 10 billion pageviews of gov.uk per year, and you shave off 20ms, thats 27 human-years of work per year. So it is certainly worth making this optimization, even if it takes a few people a few months to figure out how to remove jquery.

Re: Gov.uk drops jQuery from their front end

#110

AFAIK (I'm not a front-end dev but tried some) vanilla JavaScript has much of the things jQuery provided built-in nowadays yet they look much longer. Now as JavaScript accumulated a lot of legacy stuff only kept for compatibility + also numerous verbally/syntactically sub-elegant APIs IMHO it is time to design a language which would simply be "better JavaScript" - clean, modern and elegant yet have no fundamental dif…

> design a language which would simply be "better JavaScript" - clean, modern and elegant yet have no fundamental differences

JS' problem is that it is fundamentally not "clean, modern and elegant". It was designed in a couple of days. And all the greatness that's added to it nowadays is done in a much better design process than at it's inception, yet some choices made at it's inception can never be reversed.

I've written some Elm and think it is phenomenal at being a better JS; but it fundamentally very different. Rotten fundamentals are notoriously hard to fix; especially when a lot has been built on top and needs to keep running.

Post reply on HN