From d13bc3bee2f9bd657e1cd11051d71bac3aa9a95c Mon Sep 17 00:00:00 2001 From: Aroy-Art Date: Thu, 24 Aug 2023 17:23:56 +0200 Subject: [PATCH] Add: Code explanation --- archivist/static/js/confirm_external_links.js | 35 +++++++++++++------ 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/archivist/static/js/confirm_external_links.js b/archivist/static/js/confirm_external_links.js index 3c95b5e..948724b 100644 --- a/archivist/static/js/confirm_external_links.js +++ b/archivist/static/js/confirm_external_links.js @@ -1,29 +1,44 @@ // static/js/confirm_external_links.js +// This script is designed to handle external links on a webpage by showing a confirmation modal +// before allowing the user to navigate to the external URL. +// Wait for the DOM content to be fully loaded before executing the script. document.addEventListener("DOMContentLoaded", function () { + + // Select all anchor elements with 'href' attributes starting with 'http' that do not contain + // the current window's host domain. var externalLinks = document.querySelectorAll("a[href^='http']:not([href*='" + window.location.host + "'])"); - + + // Loop through each external link found on the page. for (var i = 0; i < externalLinks.length; i++) { + + // Attach a click event listener to each external link. externalLinks[i].addEventListener("click", function (event) { - event.preventDefault(); + event.preventDefault(); // Prevent the default behavior of clicking a link. + + // Create a Bootstrap modal instance for the external link confirmation. var confirmationModal = new bootstrap.Modal(document.getElementById('externalLinkConfirmationModal'), { - keyboard: false + keyboard: false // Disable closing the modal with the keyboard. }); + // Get references to elements within the modal. var externalLink = document.getElementById('externalLink'); var externalLinkShow = document.getElementById('externalLinkShow'); - var originalLink = this.href; - + var originalLink = this.href; // Get the original URL of the clicked external link. + + // Set the 'href' attribute of the element that will open the link. externalLink.setAttribute('href', originalLink); - + + // Update the content of an element to display the external link's URL. externalLinkShow.innerText = "URL: " + this.href; + // Show the confirmation modal. confirmationModal.show(); - + + // Attach a click event listener to the 'externalLink' element to hide the modal when clicked. externalLink.addEventListener('click', function () { - confirmationModal.hide(); - + confirmationModal.hide(); // Hide the confirmation modal. }); }); } -}); \ No newline at end of file +});