This zone is sleeping right now! ZzzZZzzz

Better not disturb it! ~You go to the petrol station and buy a slushy instead, everything is cool!~

This area will open again in 000.beats!

(Learn about Swatch Time)

Some other zones are awake :^]


Artifacts Gallery Guilds Search Wiki Login Register

Welcome, Guest. Please login or register. - Thinking of joining?
Autumn, 23 Nov 2026 - @35.25
Forum Activity: Three Stars Random | Recent Posts | Guild Recents
News: Love is not possession  :4u: Guild Events: 22: fate
+  MelonLand Forum
|-+  Da Web Revival
| |-+  ✁ ∙ Web Crafting
| | |-+  ☔︎ ∙ I need Help!
| | | |-+  Why to avoid Javascript, and when is it appropriate?


« previous next »
Pages: [1] Print Embed
Author Topic: Why to avoid Javascript, and when is it appropriate?  (Read 287 times)
Limette
Full Member ⚓︎
***
View Profile WWWArt


⛺︎ My Room
iMood: Limette
RSS: RSS

Guild Memberships:
Artifacts:
Visited on Melon's 10th Anniversary!I got robbed by Dan Q on Melonland!Joined 2025!
« on: Autumn, 22 Nov 2026 @408 » Toggle Font Embed

A common consensus on the indie web seems to be that Javascript should be avoided as much as possible. I think I get why, after all JS tends to make sites slower and stuff. Personally I avoid using JS for my website because I'm inexperienced with it and am worried about writing or copying code that I don't fully understand. It has also become a bit of a badge of honor, to be able to find pure CSS solutions to JS problems (even if I'm mostly copying other people's code snippets) and make the claim that my website is not only fully functional without- but also just genuinely doesn't use JS.

But the more I bash my head against the wall that is CSS and find myself having to give up on features where JS cannot be circumvented, I ask myself: Why am I doing this to myself? What did JS do to me, besides being harder to learn? At what point does it make more sense to use it than to not?

I ask myself this question a lot when I consider the gallery page of my website. Currently this gallery is tabbed and has "pop-ups" for the full images and their descriptions, both done fully through CSS. (Courtesy of a tutorial by Dan for the gallery and another one by... I think it was CSS-Tricks? for the tabs.) It loads really fast, I understand how all of the code works, and I'm rather proud of myself for being able to work it out... but it does come with a huge drawback, at least if I want my website to be my main avenue for sharing my art: I can't link to specific tabs or artworks. I looked into it, and it seems genuinely impossible unless I replace my pure HTML/CSS code with one that involves JS.

And so I have to wonder: what is more important to me? To have my gallery be fast, and accessible even for users that have JS disabled, or to be able to link to specific artworks from my website?

Since <noscript> exists, would it be possible to delegate my CSS-only layout to that? That way the gallery would be functional even with JS disabled, but I also get to have more specific links...

Logged

https://limette.neocities.org/assets/limette.gifhttps://file.garden/aJDdYDydqnG0q1NR/blinkies/exinc.gif
Dan Q
Hero Member ⚓︎
*****
View Profile WWWArt


I have no idea what I am doing
⛺︎ My Room
RSS: RSS

Guild Memberships:
Artifacts:
Caught by RubbereonThe Worm is creeping around Melonland profiles!Dan Q Cruisin'I DIDN'T meet Dan Q on Melonland!Visited on Melon's 10th Anniversary!
« Reply #1 on: Autumn, 22 Nov 2026 @461 » Toggle Font Embed

<noscript> ain't all that IMHO

<noscript> isn't a perfect solution, because <noscript> content only gets rendered if JavaScript is disabled. But JavaScript being disabled isn't the only reason that you'd want "no script" fallback content to be displayed. Other reasons include, for example:

- a bug in the JavaScript code halts its execution
- the JavaScript code uses features that do not work in the user's browser (e.g. new functionality, or a dependence on a not-available API)
- the JavaScript file is blocked (e.g. by a misbehaving adblocker)
- a dependency of the JavaScript fails to load (e.g. misbehaving or blocked CDN, ISP or country-level censorship, etc.)

None of these will result in the <noscript> being shown. The user just gets a broken experience: JavaScript is fundamentally more fragile than HTML and CSS, on average.

Personally, I'm not a fan of <noscript>. Here's my preferred approach: I do this:


Code
<body class="no-js">

<!--
  HTML code goes here. Any HTML that DEPENDS on JS goes in a
  <div class="needs-js">...</div>
  Any HTML that is fallback goes in a
  <div class="needs-no-js">...</div>
-->

<script>
// Essential JS goes here.

// Only when it's all run successfully do we do this:
document.body.classList.remove('no-js');
document.body.classList.add('yes-js');
</script>

</body><

With this, the <body>'s class changes from "no-js" to "yes-js". This means that we can have some CSS like this to control which interface part(s) are shown:

Code
.no-js .needs-js,
.yes-js .needs-no-js {
  display: none !important;
}

This approach is better IMHO because if your JS code crashes half-way through, you still see the no-js interface.

Logged

https://danq.me/_q26t/badges/dan-q-88x31-lighter.gif https://danq.me/_q26t/badges/dan-q-88x31-peekaboo-scroller.gif https://beige-buttons.danq.dev/beige-buttons-88x31.gif https://embed-html.danq.dev/embed-html-88x31.gif https://origami.danq.dev/88x31.gif https://skelly.monster/dem-bones-88x31.gif

Artifact Swap: Pink ham-ham rangerPolyamorousI met Dan Q on Melonland!shoeJoined 2025!cinnamoroll
Dan Q
Hero Member ⚓︎
*****
View Profile WWWArt


I have no idea what I am doing
⛺︎ My Room
RSS: RSS

Guild Memberships:
Artifacts:
Caught by RubbereonThe Worm is creeping around Melonland profiles!Dan Q Cruisin'I DIDN'T meet Dan Q on Melonland!Visited on Melon's 10th Anniversary!
« Reply #2 on: Autumn, 22 Nov 2026 @474 » Toggle Font Embed

Where I use JS

I use JS in around two circumstances:

1. Where there's no other way. skelly.monster absolutely 100% requires JavaScript. No two ways about it. Where it's not too hard, I try to make an equivalent that doesn't: e.g. when I embed one of the (JS-based) graphs from my blogging stats page into a blog post, I often embed a screenshot of that graph and replace it using JS... so that if JS fails, you still get the static image version (this is also useful for people who prefer to follow me in their RSS reader: RSS readers - for good reason - don't typically allow sites' arbitrary JavaScript to run!

2. As part of a progressive-enhancement strategy. Suppose I want an "accordion"-like interface: vertical tabs, where opening one closes the others. I can get an approximation of that using <details>/<summary> elements, and have something that works for everybody... and then I can enhance that with some JavaScript that detects when one opens and closes all the others. Most people (with working JS) will get a full accordion interface; people without JS will get a series of foldable <details> blocks. Sorted!

HTML Web Components

For that latter strategy I'm a big fan these days of what Jeremy Keith calls "HTML Web Components". The idea is that you use HTML to write as much of the functionality as you can, for maximum accessibility and usability and resilience... and then you "wrap" it in a Web Component that swaps-out the contents for something that gives more functionality!

I provide instructions for example to allow people to use my <embed-html> component in this way! A developer can write this, for example:


Code
<embed-html cache src="menu.html">
  <p>To navigate my site, <a href="menu.html">use the menu</a>.</p>
</embed-html>

...

<script type="module" src="embed-html.js"></script> 

That <embed-html> element contains a basic HTML representation: a link to a menu page. But once the embed-html.js module loads, it gets "swapped out" for the actual contents of menu.html using JavaScript. If the JavaScript works, the visitor sees the site menu. If it fails for any reason, the visitor sees a link to the site menu. Either way, they're able to fully use the site! That's the magic of HTML Web Components!

Logged

https://danq.me/_q26t/badges/dan-q-88x31-lighter.gif https://danq.me/_q26t/badges/dan-q-88x31-peekaboo-scroller.gif https://beige-buttons.danq.dev/beige-buttons-88x31.gif https://embed-html.danq.dev/embed-html-88x31.gif https://origami.danq.dev/88x31.gif https://skelly.monster/dem-bones-88x31.gif

Artifact Swap: Pink ham-ham rangerPolyamorousI met Dan Q on Melonland!shoeJoined 2025!cinnamoroll
Dan Q
Hero Member ⚓︎
*****
View Profile WWWArt


I have no idea what I am doing
⛺︎ My Room
RSS: RSS

Guild Memberships:
Artifacts:
Caught by RubbereonThe Worm is creeping around Melonland profiles!Dan Q Cruisin'I DIDN'T meet Dan Q on Melonland!Visited on Melon's 10th Anniversary!
« Reply #3 on: Autumn, 22 Nov 2026 @476 » Toggle Font Embed

But your actual question was "when is [JS] appropriate?"...

And the fundamental truth is that it's your site; so it's up to you! Don't let anybody - not the "minimal JS" nuts like me, nor the "JS everywhere" fanboys on the other side - tell you what to do with your own website! Just work to understand the limitations and make up your own mind!  :cheesy:

Logged

https://danq.me/_q26t/badges/dan-q-88x31-lighter.gif https://danq.me/_q26t/badges/dan-q-88x31-peekaboo-scroller.gif https://beige-buttons.danq.dev/beige-buttons-88x31.gif https://embed-html.danq.dev/embed-html-88x31.gif https://origami.danq.dev/88x31.gif https://skelly.monster/dem-bones-88x31.gif

Artifact Swap: Pink ham-ham rangerPolyamorousI met Dan Q on Melonland!shoeJoined 2025!cinnamoroll
Limette
Full Member ⚓︎
***
View Profile WWWArt


⛺︎ My Room
iMood: Limette
RSS: RSS

Guild Memberships:
Artifacts:
Visited on Melon's 10th Anniversary!I got robbed by Dan Q on Melonland!Joined 2025!
« Reply #4 on: Autumn, 22 Nov 2026 @495 » Toggle Font Embed

But your actual question was "when is [JS] appropriate?"...

And the fundamental truth is that it's your site; so it's up to you! Don't let anybody - not the "minimal JS" nuts like me, nor the "JS everywhere" fanboys on the other side - tell you what to do with your own website! Just work to understand the limitations and make up your own mind!  :cheesy:


When I really think about the function I want my gallery to serve, I think I won't get around having to learn some JS for it...

The reason I made this whole website, really, is because I didn't want to be bound to social media anymore when it came to hosting and sharing my art. On a personal website, I get to have full control over what I can or can't upload, and how it is presented to users. But there's an important thing that the current gallery can't do: I can't link to (or even better, embed!) specific images.

For that, I think I'll have to finally learn how to work with JS. Though, the idea of making a gallery that can have individual image links, maybe even image embeds, while also being functional if JS fails, sounds incredibly daunting. I don't even know where to begin with that, considering my JS knowledge kind of starts and ends at [alert("Hello, World!");]. I don't know how to work my way up to what I'm trying to achieve, but I guess I have a new goal for my roadmap.



Logged

https://limette.neocities.org/assets/limette.gifhttps://file.garden/aJDdYDydqnG0q1NR/blinkies/exinc.gif
Dan Q
Hero Member ⚓︎
*****
View Profile WWWArt


I have no idea what I am doing
⛺︎ My Room
RSS: RSS

Guild Memberships:
Artifacts:
Caught by RubbereonThe Worm is creeping around Melonland profiles!Dan Q Cruisin'I DIDN'T meet Dan Q on Melonland!Visited on Melon's 10th Anniversary!
« Reply #5 on: Autumn, 22 Nov 2026 @543 » Toggle Font Embed

But there's an important thing that the current gallery can't do: I can't link to (or even better, embed!) specific images.

JS for your current gallery

It sounds like what you're talking about is "deep linking": being able to have a link that, when clicked, goes to the gallery page and opens a specific image. Right?

If so: you probably can't do that using the <details>/<summary> approach you're currently using, no, because there's no way to use plain HTML+CSS to open a <details> based on the web address you went to (without e.g. something server-side like PHP, or something client-side like JS). Here's some JS that might work for you, though. Just put it at the bottom of your page, before the <body> (because you want the contents of the page to have be loaded first!):


Code
<script>
// Attempt to get the bit after the hash (#) in the URL:
const requestedUrl = window.location.hash.slice(1);
// If we got one, and it's not blank...
if(requestedUrl != '') {
  // ...then try to find the first <details> that contains an <img>
  // whose src ENDS with ($=) the thing after the hash
  const requestedDetails = document.querySelector(`details:has(img[src$="${requestedUrl}"])`);
  // If we successfully found such a <details>; open it!
  if(requestedDetails) requestedDetails.open = true;
}
</script>

With that code in place, you can go to e.g. https://limette.neocities.org/gallery#260615-1.webp and it'll automatically open that picture, just as if it had been clicked on. Users with JS disabled just see the gallery as normal.

A complete HTML+CSS-only solution?

It is possible to do this entirely in HTML+CSS, though. Instead of using the <details>/<summary> pattern, use the Popover API - here's an example that does just images, but it works just the same for a dialog/popover that's images and text, like yours. Ensure that each popover has an id="...". Then, use the CSS ":target" selector to say "if the ID of the popover appears after the hash in the URL, display that popover". That CSS is likely to be something like this (I've not tested it, but it's a starting point):


Code
/* a thing of class="lightbox" whose id="..." appears after the hash in the address bar... */
.lightbox:target {
  /* is forced to be visible: */
  display: block;
}

Logged

https://danq.me/_q26t/badges/dan-q-88x31-lighter.gif https://danq.me/_q26t/badges/dan-q-88x31-peekaboo-scroller.gif https://beige-buttons.danq.dev/beige-buttons-88x31.gif https://embed-html.danq.dev/embed-html-88x31.gif https://origami.danq.dev/88x31.gif https://skelly.monster/dem-bones-88x31.gif

Artifact Swap: Pink ham-ham rangerPolyamorousI met Dan Q on Melonland!shoeJoined 2025!cinnamoroll
Limette
Full Member ⚓︎
***
View Profile WWWArt


⛺︎ My Room
iMood: Limette
RSS: RSS

Guild Memberships:
Artifacts:
Visited on Melon's 10th Anniversary!I got robbed by Dan Q on Melonland!Joined 2025!
« Reply #6 on: Autumn, 22 Nov 2026 @624 » Toggle Font Embed

JS for your current gallery

You are wayyy too helpful, it feels almost bad to be getting such well explained and individual help for free. It took me a couple tries until I could grasp what the code was doing, but I understand it now! It's not very complex, thanks.

I tried this script out on the gallery, and it works really well, except for one small issue: If I try to point a link towards an image that's in an inactive tab (right now, that's only the 2025 tab), then the image won't be visible until I manually select that tab first. (You can test it by opening this link: https://limette.neocities.org/gallery#251026.webp and then selecting "2025") I suppose the solution to that would be to make the script also select the tab corresponding to each image...

Code
radiobtn = document.getElementById("ID");
radiobtn.checked = true;

This would be the code for that, if I'm not mistaken?

Of course, the problem here is that the script needs to know which button (tab) to check. The two methods I can think of are to either add another url parameter that determines which tab should be active, or to give each image a class that corresponds to its respective tab, and have the script look for that. The first option seems more efficient. If I had any JS experience at all I could probably edit the script myself to do this, but alas, being able to read a script and being able to write one aren't the same kind of skill. I'm trying my best to look into it, though.

« Last Edit: Autumn, 22 Nov 2026 @634 by Limette » Logged

https://limette.neocities.org/assets/limette.gifhttps://file.garden/aJDdYDydqnG0q1NR/blinkies/exinc.gif
Dan Q
Hero Member ⚓︎
*****
View Profile WWWArt


I have no idea what I am doing
⛺︎ My Room
RSS: RSS

Guild Memberships:
Artifacts:
Caught by RubbereonThe Worm is creeping around Melonland profiles!Dan Q Cruisin'I DIDN'T meet Dan Q on Melonland!Visited on Melon's 10th Anniversary!
« Reply #7 on: Autumn, 22 Nov 2026 @726 » Toggle Font Embed

You're absolutely right. Two options.

Option 1: encode both the tab and image into the URL

One would be yes to have the link end with something like #2025_251026.webp. Use some character (I'm using _) to separate the "tab" part from the "image" part, and then "split" the string on this and hit the tab accordingly. Maybe like this:


Code
<script>
// Attempt to get the bit after the hash (#) in the URL:
const requestedUrl = window.location.hash.slice(1);

// If we got one, and it's not blank...
if(requestedUrl != '') {
  // Then split it by _ and ensure we get two parts:
  const requestedUrlParts = requestedUrl.split('_');
  if(requestedUrlParts.length == 2) {
    // Let's name the two parts to make them easy to understand:
    const requestedTabYear = requestedUrlParts[0]; // first part
    const requestedImage = requestedUrlParts[1]; // second part

    // Try to find a tab <input> whose ID matches the first part:
    const tabInput = document.getElementById(`tab-${requestedTabYear}-toggle`);
    // Try to find the requested image's <details> too:
    const requestedDetails = document.querySelector(`details:has(img[src$="${requestedImage}"])`);

    // So long as BOTH were successfully found, switch tab and open the image:
    if(tabInput && requestedDetails) {
      tabInput.checked = true;
      requestedDetails.open = true;
    }
  }
}
</script>

Then you can use URLs like this: https://limette.neocities.org/gallery#2025_251026.webp

Option 2: have the JavaScript WORK OUT which tab is needed

You're right that this approach would be more computationally-intensive... but it's not by MUCH, and it keeps your URLs moderately clean. How about this...


Code
<script>
// Attempt to get the bit after the hash (#) in the URL:
const requestedUrl = window.location.hash.slice(1);

// If we got one, and it's not blank...
if(requestedUrl != '') {
  // ...then try to find the first <details> that contains an <img>
  // whose src ENDS with ($=) the thing after the hash
  const requestedDetails = document.querySelector(`details:has(img[src$="${requestedUrl}"])`);

  // If we successfully found such a <details>...
  if(requestedDetails) {
    // Work out which tab this <details> is on. It will be in a <section class="tab-content" id="tab-YYYY-content">
    // where YYYY is a year:
    const section = requestedDetails.closest('section.tab-content');

    // To get the year, let's just split the <section>'s ID
    // (we could use fancy things like regular expressions, but this is Good Enough)
    const sectionIdParts = section.id.split('-');
    const year = sectionIdParts[1];

    // Now we can safety-check that we found what we expected...
    if((sectionIdParts[0] == 'tab') && (sectionIdParts[2] == 'content')) {
      // Then find the tab <input> we need to hit:
      const tabInput = document.getElementById(`tab-${year}-toggle`);

      // And so long as we've got a tab and details to open, do it:
      if(tabInput && requestedDetails) {
        tabInput.checked = true;
        requestedDetails.open = true;
      }
    }
  }
}
</script>

This approach builds on what you already have and means you don't need to make the URLs any longer (or have to think about what happens if somebody CHANGES what's in their address bar to a year and image that don't match one another!).

Note that my code is excessively defensive. It finds the right <section> and then double-checks its ID matches the expected format, for example. This is probably overkill!

Logged

https://danq.me/_q26t/badges/dan-q-88x31-lighter.gif https://danq.me/_q26t/badges/dan-q-88x31-peekaboo-scroller.gif https://beige-buttons.danq.dev/beige-buttons-88x31.gif https://embed-html.danq.dev/embed-html-88x31.gif https://origami.danq.dev/88x31.gif https://skelly.monster/dem-bones-88x31.gif

Artifact Swap: Pink ham-ham rangerPolyamorousI met Dan Q on Melonland!shoeJoined 2025!cinnamoroll
Limette
Full Member ⚓︎
***
View Profile WWWArt


⛺︎ My Room
iMood: Limette
RSS: RSS

Guild Memberships:
Artifacts:
Visited on Melon's 10th Anniversary!I got robbed by Dan Q on Melonland!Joined 2025!
« Reply #8 on: Autumn, 22 Nov 2026 @852 » Toggle Font Embed

@Dan Q waaaa thank you! The second script is perfect. This is going to make it a lot easier for me to share my art with people, since the people that I want to share it with don't regularly check out my website, and having direct links removes a lot of friction. (If only I could get them into RSS...) AND everything still functions without JS, the worst that can happen is that a link breaks.

Once I get around to overhauling my Links page you're probably going to get extra credit for the amount of help you've been for making this gallery work. I think I'd still be pushing the project off if you hadn't posted that initial gallery tutorial.

Logged

https://limette.neocities.org/assets/limette.gifhttps://file.garden/aJDdYDydqnG0q1NR/blinkies/exinc.gif
Pages: [1] Print Embed 
« previous next »
 

Melonking.Net © Always and ever was! SMF © 2021 | Privacy Notice | Send Feedback | Supporters ♥ Guide | Rules | Whos a Mod? | RSS | WAP | Mobile


MelonLand Badges and Other Melon Sites!

MelonLand Project! Visit the MelonLand Forum! Support the Forum
Visit Melonking.Net! Visit the Gif Gallery! Pixel Sea TamaNOTchi
@000 Melon
Land
Forum Art Hub Chat Webring Explore Random Recent Tenement Arcade Want to Login or Join ? Web Craft Guide Graphic Catalogue Wiki Help I'm Lost! Link Land Newsletters Image Stream Minecraft: Online
Zap!
Passport Sites tamaNOTchi Asatte Add your site?
Aero Archive Onio Cafe 32 Bit Cafe Status Cafe