aboutsummaryrefslogtreecommitdiff
path: root/build/obvious-js-injection-fallback/index.html
blob: 79fb611234227ed6cff2389a768b03b9b97d02e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!doctype html>
<html lang="en" id="top">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="icon" href="data:,">
	<title>Obvious Javascript 'Injection' Fallback</title>
	<link href="https://bt.ht/atom.xml" type="application/atom+xml" rel="alternate" title="Atom feed for blog posts" />
	<style>*{box-sizing:border-box;}body{font-family:sans-serif;margin:0 auto;max-width:650px;padding:1rem;}img{max-width:100%;}pre{overflow:auto;}table{text-align:left;width:100%;}</style>
</head>

<nav>
  <a href="#menu">Menu &darr;</a>
</nav>

<main>
<h1>Obvious Javascript 'Injection' Fallback</h1>
<p>2020-12-04</p>
<p>Sometimes websites and web apps might require content to be "injected" via Javascript. I should mention that I am strongly against this practice - but often this kind of thing is out of one's hands. So, the least I can do is setup these "injections" to have proper fallbacks for users who disable JS. You would be surprised how many developers build <em>empty</em> HTML elements with the assumption they will be filled via Javascript.</p>
<h2>Our Hypothetical Project</h2>
<p>Let's pretend that we have a total tally that pulls in the number of current users using our fake SaaS app. We would do something like this:</p>
<h3>HTML</h3>
<p>Here we create an empty <code>h2</code> tag that will update with the current number of users via js:</p>
<pre><code>&lt;main&gt;
    &lt;h2 class=&quot;total-tally&quot;&gt;&lt;/h2&gt;
&lt;/main&gt;
</code></pre>
<h3>Javascript</h3>
<p>You'll have to use your imagination here and assume that the <code>totalTally</code> variable pulls in the numbers dynamically via API:</p>
<pre><code>var totalTally = &quot;273,677&quot; /* This would pull something dynamically in prod */
document.getElementsByClassName(&quot;total-tally&quot;)[0].innerHTML=totalTally;
</code></pre>
<h2>The Problem</h2>
<p>The big issue we have now occurs when a user visits this page without JS enabled. The <code>h2</code> tag will remain empty and they won't see anything. I know this seems like a very avoidable issue, but you would be surprised how often it actually happens on the web.</p>
<h2>The (overly simple) Solution</h2>
<p>The easiest way to avoid these types of empty tags - add static content. I know - mind blowing, right?</p>
<h3>HTML (updated)</h3>
<pre><code>&lt;main&gt;
    &lt;h2 class=&quot;total-tally&quot;&gt;200,000+&lt;/h2&gt;
&lt;/main&gt;
</code></pre>
<p>You might be reading this and saying to yourself, "Wow! Thanks Captain Obvious!" and that's a fair reaction. This is an <em>obvious</em> demo on purpose. If even one single reader learns to avoid leaving empty HTML tags that are solely dependent on Javascript injection, then I'd say this demo was a huge success.</p>
<p>Rule of thumb: don't make assumption about your users. Play it safe.</p>
<footer role="contentinfo">
    <h2>Menu Navigation</h2>
    <ul id="menu">
        <li><a href="/">Home</a></li>
        <li><a href="/projects">Projects</a></li>
        <li><a href="/uses">Uses</a></li>
        <li><a href="/wiki">Wiki</a></li>
        <li><a href="/resume">Resume</a></li>
        <li><a href="/colophon">Colophon</a></li>
        <li><a href="/now">Now</a></li>
        <li><a href="/donate">Donate</a></li>
        <li><a href="/atom.xml">RSS</a></li>
        <li><a href="#top">&uarr; Top of the page</a></li>
    </ul>
    <small>
        Built with <a href="https://git.sr.ht/~bt/barf">barf</a>. <br>
        Maintained with ♥ for the web. <br>
        Proud supporter of <a href="https://usefathom.com/ref/DKHJVX">Fathom</a> &amp; <a href="https://nextdns.io/?from=74d3p3h8">NextDNS</a>. <br>
        The content for this site is <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>.<br> The <a href="https://git.sr.ht/~bt/bt.ht">code for this site</a> is <a href="https://git.sr.ht/~bt/bt.ht/tree/master/item/LICENSE">MIT</a>.
    </small>
</footer>