Skip to content

Instantly share code, notes, and snippets.

@SankaitLaroiya
Created March 9, 2018 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SankaitLaroiya/b80c226e2700c4eddee7a53048d035b5 to your computer and use it in GitHub Desktop.
Save SankaitLaroiya/b80c226e2700c4eddee7a53048d035b5 to your computer and use it in GitHub Desktop.
(JS) Get the host name from a URL
// Code snippet taken from:
// https://stackoverflow.com/questions/8498592/extract-hostname-name-from-string
function getHostNameFromURL(url) {
if (!url) {
return null;
}
let hostname;
if (url.indexOf("://") > -1) {
hostname = url.split('/')[2];
} else {
hostname = url.split('/')[0];
}
hostname = hostname.split(':')[0];
hostname = hostname.split('?')[0];
return hostname;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment