Gallery-Archivist/archivist/static/js/confirm_external_links.js

45 lines
2.1 KiB
JavaScript
Raw Normal View History

// static/js/confirm_external_links.js
2023-08-24 17:23:56 +02:00
// 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.
2023-08-24 17:23:56 +02:00
// Wait for the DOM content to be fully loaded before executing the script.
document.addEventListener("DOMContentLoaded", function () {
2023-08-24 17:23:56 +02:00
// 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 + "'])");
2023-08-24 17:23:56 +02:00
// Loop through each external link found on the page.
for (var i = 0; i < externalLinks.length; i++) {
2023-08-24 17:23:56 +02:00
// Attach a click event listener to each external link.
externalLinks[i].addEventListener("click", function (event) {
2023-08-24 17:23:56 +02:00
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'), {
2023-08-24 17:23:56 +02:00
keyboard: false // Disable closing the modal with the keyboard.
});
2023-08-24 17:23:56 +02:00
// Get references to elements within the modal.
var externalLink = document.getElementById('externalLink');
var externalLinkShow = document.getElementById('externalLinkShow');
2023-08-24 17:23:56 +02:00
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);
2023-08-24 17:23:56 +02:00
// Update the content of an element to display the external link's URL.
externalLinkShow.innerText = "URL: " + this.href;
2023-08-24 17:23:56 +02:00
// Show the confirmation modal.
confirmationModal.show();
2023-08-24 17:23:56 +02:00
// Attach a click event listener to the 'externalLink' element to hide the modal when clicked.
externalLink.addEventListener('click', function () {
2023-08-24 17:23:56 +02:00
confirmationModal.hide(); // Hide the confirmation modal.
});
});
}
2023-08-24 17:23:56 +02:00
});