Add: Code explanation
This commit is contained in:
parent
a0aba71291
commit
d13bc3bee2
1 changed files with 25 additions and 10 deletions
|
@ -1,28 +1,43 @@
|
|||
// 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.
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue