Add: Code explanation

This commit is contained in:
Aroy-Art 2023-08-24 17:23:56 +02:00
parent a0aba71291
commit d13bc3bee2
Signed by: Aroy
GPG key ID: 583642324A1D2070

View file

@ -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.
});
});
}
});
});