Created
March 9, 2018 21:52
-
-
Save SankaitLaroiya/b80c226e2700c4eddee7a53048d035b5 to your computer and use it in GitHub Desktop.
(JS) Get the host name from a URL
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
// 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