Created
July 12, 2019 08:49
-
-
Save nc7s/fbce9341000fc0d8989903abc602edbd to your computer and use it in GitHub Desktop.
Decide if a given href is internal link
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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