From 54410253ff1550c825a10a276d3022cb9897f040 Mon Sep 17 00:00:00 2001 From: earnest ma Date: Wed, 1 Dec 2021 09:35:45 -0500 Subject: [PATCH] feat: Open external links in a new window --- assets/js/new-tabs.js | 28 ++++++++++++++++++++++++++++ layouts/_default/baseof.html | 2 ++ 2 files changed, 30 insertions(+) create mode 100644 assets/js/new-tabs.js diff --git a/assets/js/new-tabs.js b/assets/js/new-tabs.js new file mode 100644 index 0000000..27b8c83 --- /dev/null +++ b/assets/js/new-tabs.js @@ -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(); diff --git a/layouts/_default/baseof.html b/layouts/_default/baseof.html index 06d3fbb..35f2c57 100644 --- a/layouts/_default/baseof.html +++ b/layouts/_default/baseof.html @@ -12,6 +12,8 @@ + {{- $newtabjs := resources.Get "/js/new-tabs.js" | js.Build "/js/new-tabs.js" | minify | fingerprint }} + \ No newline at end of file