Skip to content

Instantly share code, notes, and snippets.

@nc7s
Created July 12, 2019 08:49
Show Gist options
  • Save nc7s/fbce9341000fc0d8989903abc602edbd to your computer and use it in GitHub Desktop.
Save nc7s/fbce9341000fc0d8989903abc602edbd to your computer and use it in GitHub Desktop.
Decide if a given href is internal link
function isInternalLink(href) {
try {
/* full URL i.e. has protocol */
let url = new URL(href)
return url.origin === window.location.origin
} catch {
return !href.startsWith('//') ||
(href.startsWith('//') && (window.location.protocol + href).startsWith(window.location.origin))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment