Skip to content

Commit 800c382

Browse files
authored
feat: open external links in new tab (#1222)
1 parent ea6bfb7 commit 800c382

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

_includes/head.html

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
<link rel="stylesheet" href="/public/css/index.css">
5656
<!-- JavaScript -->
5757
<script src="/public/js/switch-alt-link.js" async defer></script>
58+
<script src="/public/js/external-links-new-tab.js" async defer></script>
5859
{% include ogp.html %}
5960
{% include google_analytics.html %}
6061
</head>

public/js/external-links-new-tab.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function onReady() {
2+
return new Promise((resolve) => {
3+
const readyState = document.readyState;
4+
if (readyState === "interactive" || readyState === "complete") {
5+
resolve();
6+
} else {
7+
window.addEventListener("DOMContentLoaded", resolve);
8+
}
9+
});
10+
}
11+
12+
// If it is an external link, set target="_blank"
13+
function setExternalLinksOpenInNewTabs() {
14+
var externalLinkNodeList = document.querySelectorAll("a[href^='http']:not([href*='jser.info'])")
15+
var links = Array.prototype.slice.call(externalLinkNodeList);
16+
// add _blank and track
17+
links.forEach(function (link) {
18+
if (!link.target) {
19+
link.target = "_blank";
20+
}
21+
});
22+
}
23+
24+
onReady().then(() => setExternalLinksOpenInNewTabs());

0 commit comments

Comments
 (0)