Skip to content

Commit

Permalink
feat: open external links in new tab (#1222)
Browse files Browse the repository at this point in the history
  • Loading branch information
k35o authored Sep 13, 2024
1 parent ea6bfb7 commit 800c382
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions _includes/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<link rel="stylesheet" href="/public/css/index.css">
<!-- JavaScript -->
<script src="/public/js/switch-alt-link.js" async defer></script>
<script src="/public/js/external-links-new-tab.js" async defer></script>
{% include ogp.html %}
{% include google_analytics.html %}
</head>
24 changes: 24 additions & 0 deletions public/js/external-links-new-tab.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function onReady() {
return new Promise((resolve) => {
const readyState = document.readyState;
if (readyState === "interactive" || readyState === "complete") {
resolve();
} else {
window.addEventListener("DOMContentLoaded", resolve);
}
});
}

// If it is an external link, set target="_blank"
function setExternalLinksOpenInNewTabs() {
var externalLinkNodeList = document.querySelectorAll("a[href^='http']:not([href*='jser.info'])")
var links = Array.prototype.slice.call(externalLinkNodeList);
// add _blank and track
links.forEach(function (link) {
if (!link.target) {
link.target = "_blank";
}
});
}

onReady().then(() => setExternalLinksOpenInNewTabs());

0 comments on commit 800c382

Please sign in to comment.