2023-08-24 17:19:12 +02:00
|
|
|
// 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:19:12 +02:00
|
|
|
|
2023-08-24 17:23:56 +02:00
|
|
|
// Wait for the DOM content to be fully loaded before executing the script.
|
2023-08-24 17:19:12 +02:00
|
|
|
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.
|
2023-08-24 17:19:12 +02:00
|
|
|
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.
|
2023-08-24 17:19:12 +02:00
|
|
|
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.
|
2023-08-24 17:19:12 +02:00
|
|
|
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.
|
2023-08-24 17:19:12 +02:00
|
|
|
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:19:12 +02:00
|
|
|
});
|
|
|
|
|
2023-08-24 17:23:56 +02:00
|
|
|
// Get references to elements within the modal.
|
2023-08-24 17:19:12 +02:00
|
|
|
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.
|
2023-08-24 17:19:12 +02:00
|
|
|
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.
|
2023-08-24 17:19:12 +02:00
|
|
|
externalLinkShow.innerText = "URL: " + this.href;
|
|
|
|
|
2023-08-24 17:23:56 +02:00
|
|
|
// Show the confirmation modal.
|
2023-08-24 17:19:12 +02:00
|
|
|
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.
|
2023-08-24 17:19:12 +02:00
|
|
|
externalLink.addEventListener('click', function () {
|
2023-08-24 17:23:56 +02:00
|
|
|
confirmationModal.hide(); // Hide the confirmation modal.
|
2023-08-24 17:19:12 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2023-08-24 17:23:56 +02:00
|
|
|
});
|