From a771136761db0d1beae208d599b89384b58fc1ff Mon Sep 17 00:00:00 2001 From: rbalsleyMSFT <53497092+rbalsleyMSFT@users.noreply.github.com> Date: Mon, 9 Feb 2026 16:59:42 -0800 Subject: [PATCH] Adds external link handling script Improves documentation UX by enabling consistent behavior for outbound links across pages --- docs/_includes/head_custom.html | 3 +- docs/assets/js/external-links.js | 91 ++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 docs/assets/js/external-links.js diff --git a/docs/_includes/head_custom.html b/docs/_includes/head_custom.html index 4ab9861..7c50492 100644 --- a/docs/_includes/head_custom.html +++ b/docs/_includes/head_custom.html @@ -282,4 +282,5 @@ - \ No newline at end of file + + \ No newline at end of file diff --git a/docs/assets/js/external-links.js b/docs/assets/js/external-links.js new file mode 100644 index 0000000..09618d0 --- /dev/null +++ b/docs/assets/js/external-links.js @@ -0,0 +1,91 @@ +(function () { + 'use strict'; + + function HasToken(tokens, token) { + for (var i = 0; i < tokens.length; i++) { + if (tokens[i] === token) { + return true; + } + } + + return false; + } + + function AddRelToken(anchor, token) { + var rel = (anchor.getAttribute('rel') || '').trim(); + var tokens = rel ? rel.split(/\s+/) : []; + + if (!HasToken(tokens, token)) { + tokens.push(token); + } + + anchor.setAttribute('rel', tokens.join(' ').trim()); + } + + function IsExternalHttpLink(url) { + if (!url) { + return false; + } + + if (url.protocol !== 'http:' && url.protocol !== 'https:') { + return false; + } + + return url.origin !== window.location.origin; + } + + function InitExternalLinksNewTab() { + var mainContent = document.querySelector('.main-content'); + if (!mainContent) { + return; + } + + var anchors = mainContent.querySelectorAll('a[href]'); + for (var i = 0; i < anchors.length; i++) { + var anchor = anchors[i]; + var href = (anchor.getAttribute('href') || '').trim(); + + if (!href) { + continue; + } + + if (href.charAt(0) === '#') { + continue; + } + + if (href.indexOf('mailto:') === 0 || href.indexOf('tel:') === 0 || href.indexOf('javascript:') === 0) { + continue; + } + + var url = null; + try { + url = new URL(href, window.location.href); + } catch (e) { + continue; + } + + if (!IsExternalHttpLink(url)) { + continue; + } + + var target = (anchor.getAttribute('target') || '').trim(); + + if (!target) { + anchor.setAttribute('target', '_blank'); + target = '_blank'; + } + + if (target === '_blank') { + AddRelToken(anchor, 'noopener'); + AddRelToken(anchor, 'noreferrer'); + } + } + } + + if (document.readyState === 'loading') { + document.addEventListener('DOMContentLoaded', InitExternalLinksNewTab); + return; + } + + InitExternalLinksNewTab(); +})(); \ No newline at end of file