Skip to content

Commit

Permalink
migliorato js
Browse files Browse the repository at this point in the history
  • Loading branch information
aborruso committed May 3, 2024
1 parent b06a9a9 commit 0ffdf0e
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions docs/javascript/extra.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
// Open links externally.
var links = document.links;

for (var i = 0, linksLength = links.length; i < linksLength; i++) {
if (links[i].hostname != window.location.hostname) {
links[i].target = '_blank';
}
}
// Funzione per aprire in una nuova scheda tutti i link esterni e i link ai file PDF
function targetBlankForExternalAndPDFs() {
// Ottieni l'host del sito corrente senza sottodomini
var internal = location.hostname.replace("www.", "");
var internalRegex = new RegExp(internal, "i");

// Aggiungi un'espressione regolare per riconoscere anche gli indirizzi IP locali
var localHost = /localhost|127\.0\.0\.1/;

// Prendi tutti i link presenti nella pagina
var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
var href = links[i].href; // Ottieni l'URL di ogni link

// Controlla se l'URL è considerato interno o esterno, e se punta a un file PDF
if ((internalRegex.test(links[i].hostname) || localHost.test(links[i].hostname)) && href.endsWith('.pdf')) {
// Se il link è interno (incluso localhost o IP locale) e punta a un PDF, apri in una nuova scheda
links[i].setAttribute('target', '_blank');
} else if (!internalRegex.test(links[i].hostname) && !localHost.test(links[i].hostname) || href.endsWith('.pdf')) {
// Se il link è esterno o punta a un PDF, apri in una nuova scheda
links[i].setAttribute('target', '_blank');
}
}
};

// Esegui la funzione per applicare la regola ai link
targetBlankForExternalAndPDFs();

0 comments on commit 0ffdf0e

Please sign in to comment.