c3re-website/themes/hugo-theme-flat/static/js/href_handling.js

20 lines
736 B
JavaScript
Raw Normal View History

2024-01-18 14:26:34 +01:00
document.addEventListener("DOMContentLoaded", function () {
document.querySelectorAll("a").forEach(function (element) {
2024-01-09 13:05:30 +01:00
const myhost = window.location.hostname;
2024-01-18 14:26:34 +01:00
const linkhost = new URL(element.href).hostname;
if (myhost === linkhost) {
let prefetchUrl = new URL(element.href).toString();
// lest make a prefetch tag for this url
let link = document.createElement("link");
link.setAttribute("rel", "prefetch");
link.setAttribute("href", prefetchUrl);
link.setAttribute("as", "document");
document.head.appendChild(link);
element.classList.add("internal");
} else {
element.setAttribute("target", "_blank");
element.classList.add("external");
2024-01-09 13:05:30 +01:00
}
2024-01-18 14:26:34 +01:00
});
});