PageSourceSearch

https://jparedesds.github.io/js/external-links.js

js jparedesds.github.io collected 2026-10-03 09:25:27 UTC 940 bytes, 30 lines download raw bytes

1// Open external links (and PDFs) in a new tab.
2// Applies to every link in the page content, so new portfolio entries
3// added to index.md are covered automatically.
4(function () {
5  function isExternal(link) {
6    return link.hostname && link.hostname !== window.location.hostname;
7  }
8
9  function isPdf(link) {
10    return /\.pdf($|\?|#)/i.test(link.pathname || '');
11  }
12
13  document.addEventListener('DOMContentLoaded', function () {
14    var links = document.querySelectorAll('.content a[href]');
15
16    Array.prototype.forEach.call(links, function (link) {
17      var href = link.getAttribute('href') || '';
18
19      // Leave in-page anchors, mail and phone links alone.
20      if (href.charAt(0) === '#' || /^(mailto:|tel:)/i.test(href)) {
21        return;
22      }
23
24      if (isExternal(link) || isPdf(link)) {
25        link.setAttribute('target', '_blank');
26        link.setAttribute('rel', 'noopener noreferrer');
27      }
28    });
29  });
30})();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.