-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexternal-links.js
More file actions
20 lines (17 loc) · 799 Bytes
/
Copy pathexternal-links.js
File metadata and controls
20 lines (17 loc) · 799 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Opens every external link (a different host than this site) in a new tab.
// Internal navigation (nav, home, "Read More", etc.) is left untouched since
// its href resolves to the same hostname. Runs once on load, so it also
// covers links inside blog post content without editing every post.
document.addEventListener("DOMContentLoaded", function () {
var here = window.location.hostname;
document.querySelectorAll("a[href]").forEach(function (link) {
var href = link.getAttribute("href");
if (!href || href.charAt(0) === "#" || href.indexOf("mailto:") === 0 || href.indexOf("tel:") === 0) {
return;
}
if (link.hostname && link.hostname !== here) {
link.setAttribute("target", "_blank");
link.setAttribute("rel", "noopener noreferrer");
}
});
});