From 5bd73b97127ec9238e38a36950a007da23c4fa22 Mon Sep 17 00:00:00 2001 From: gmd85 Date: Thu, 7 Apr 2022 01:42:04 +0200 Subject: [PATCH] Fix JavaScript error when opening images --- app/static/js/utils.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/app/static/js/utils.js b/app/static/js/utils.js index d13bac9..6a8f2ce 100644 --- a/app/static/js/utils.js +++ b/app/static/js/utils.js @@ -1,6 +1,10 @@ const checkForTracking = () => { const mainDiv = document.getElementById("main"); - const query = document.getElementById("search-bar").value.replace(/\s+/g, ''); + const searchBar = document.getElementById("search-bar"); + // some pages (e.g. images) do not have these + if (!mainDiv || !searchBar) + return; + const query = searchBar.value.replace(/\s+/g, ''); // Note: regex functions for checking for tracking queries were derived // from here -- https://stackoverflow.com/questions/619977 @@ -59,11 +63,14 @@ document.addEventListener("DOMContentLoaded", function() { checkForTracking(); // Clear input if reset button tapped - const search = document.getElementById("search-bar"); + const searchBar = document.getElementById("search-bar"); const resetBtn = document.getElementById("search-reset"); + // some pages (e.g. images) do not have these + if (!searchBar || !resetBtn) + return; resetBtn.addEventListener("click", event => { event.preventDefault(); - search.value = ""; - search.focus(); + searchBar.value = ""; + searchBar.focus(); }); });