by Syonyk · 4y ago · 0 comments
I learned this recently on my personal site when I was going through and de-javascripting stuff. I more and more frequently run without scripting enabled, and it felt very wrong that my personal site had some JS requirements. The worst being that it wouldn't render without JS due to a slick little fade-in effect in the template...
One of the things I do quite like (living on a low bandwidth connection a lot of the time) is lazy loading - and it turns out that you can do that without JS too, now, at least in modern browsers.
img loading="{lazy,eager}" will do this without needing JS. Combine it with picturesets for smaller images (you let the browser pick an image it supports and a size relevant to the display), and life is good without any JS.
At this point, as far as I know, the only things on my site that require JS are the "Make the image bigger when you click on it" feature, and then a JS based search function (which sucks down an awful lot of JSON, but it's fine, and beats an active server backend for a personal blog).
It's also impressive just how responsive a browser on a gutless wonder ARM SBC is when all it's having to do is render content, not run a bunch of JS!
You could actually do this without JS, just wrap the image in a label for an invisible input, and apply transform with a transition based on :focus or :focus-within.
It would be ugly, sure, but so much of modern front-end development is already ugly.
"Make the image bigger when you click on it" also can be done without JavaScripts, in a few ways: One way is to just use a ordinary hyperlink directly to the image (and optionally you can override it if JavaScripts are enabled), another way is to just to use features that the web browser might have (if the user has them, they can be used; if not, then not), and a third way might be to use CSS (although I generally want to avoid CSS requirements, too).
Fade-in effects can also be done using CSS, but may be undesirable (I am one who would prefer to reduce animation as much as possible, and other users too).
For JS based search functions with JSON, you could do something that I mentioned before which is <link rel="data"> and the user can make their own search functions if wanted (maybe a browser or extension might have a JSON searching function with many options (that the web page might not necessarily implement; I do often find wanted to make queries that the web page does not implement)), but if JavaScripts are enabled then it will use that one.
Even if the data is rendered into HTML in server-side scripts or generated static pages, you can still have <link rel="data"> or <a rel="data"> to allow to access the raw data, too, since it will sometimes be useful.
But in any case, you should almost always ensure that it works properly with JavaScripts disabled, and usually should work with CSS disabled too. Use <noscript> blocks if needed, and include API documentation or download links or whatever, not simply "This requires JavaScripts to work". For some types of web apps (e.g. interactive games), it might require JavaScripts but even then you can include links to documentation, and if it is open-source also source codes, even if scripts are disabled.
In general, in my opinion, most web pages should need neither JavaScripts nor CSS (and often does not need pictures either).
Pictures aren't an option for my blog. It's very photo heavy as it's a lot of teardowns and analysis, and the bulk of my hacking effort has been ensuring that all the photos are run through the render pipeline and into photosets, to reduce bandwidth. I've debated adding AVIF images, but render time on those is quite obscene compared to jpg/png/webp. I should probably add fallback hyperlinks to the original image if JS is disabled - for now it just displays them inline.
I can't help imagining that the correct fall-back implementation here is to make the game turn-based instead of real-time, although even a game as simple as Pong might lose its appeal under that constraint.
The irony is, modern browsers are more likely to have JS enabled, older browsers are less likely to support lazy loading or JS.
"Site doesn't work with JS disabled" is a problem, and I fixed it.
"Site looks weird in a 20 year old browser" is someone else's problem. :)
On a website linked in the article, there is something if you search for "Lightbox", that comes with a codepen: http://youmightnotneedjs.com/
I was saying that "You don't need Javascript to do lazy loading," not making any claims about the privacy or lack thereof.