feat: Open external links in a new window

main v0.2.0
earnest ma 2021-12-01 09:35:45 -05:00
parent edc8d00a95
commit 54410253ff
Signed by: earnest ma
GPG Key ID: A343F43342EB6E2A
2 changed files with 30 additions and 0 deletions

28
assets/js/new-tabs.js Normal file
View File

@ -0,0 +1,28 @@
// https://hugocodex.org/add-ons/new-window-fix
//open external links in a new window
function external_new_window() {
for(var c = document.getElementsByTagName("a"), a = 0;a < c.length;a++) {
var b = c[a];
if(b.getAttribute("href") && b.hostname !== location.hostname) {
b.target = "_blank";
b.rel = "noopener";
}
}
}
//open PDF links in a new window
function pdf_new_window ()
{
if (!document.getElementsByTagName) return false;
var links = document.getElementsByTagName("a");
for (var eleLink=0; eleLink < links.length; eleLink ++) {
if ((links[eleLink].href.indexOf('.pdf') !== -1)||(links[eleLink].href.indexOf('.doc') !== -1)||(links[eleLink].href.indexOf('.docx') !== -1)) {
links[eleLink].onclick =
function() {
window.open(this.href);
return false;
}
}
}
}
pdf_new_window();
external_new_window();

View File

@ -12,6 +12,8 @@
</div>
<footer>{{ partial "footer.html" . -}}</footer>
{{- $newtabjs := resources.Get "/js/new-tabs.js" | js.Build "/js/new-tabs.js" | minify | fingerprint }}
<script src="{{ $newtabjs.Permalink }}"></script>
</body>
</html>