diff --git a/_templates/404.html b/_templates/404.html
index e2431403bf6cf3975192402980adda34ff424090..ebac32c6433474fe689ea1cf0728bf94fd709858 100644
--- a/_templates/404.html
+++ b/_templates/404.html
@@ -105,14 +105,16 @@
         }
     }
 
-    if (!redirectFound) {
-        // In case the link test doesn't include redirectFolders
-        const headingElement = document.getElementById('pageHeading');
-        if (headingElement) {
-            headingElement.textContent = "Page Not Found (404)";
+    document.addEventListener("DOMContentLoaded", function () {
+        if (!redirectFound) {
+            // In case the link test doesn't include redirectFolders
+            const headingElement = document.getElementById('pageHeading');
+            if (headingElement) {
+                headingElement.textContent = "Page Not Found (404)";
+            }
+            document.getElementById('redirectInfo').style.display = "block";
         }
-        document.getElementById('redirectInfo').style.display = "block";
-    }
+    });
 </script>
 
 {{ super() }}