How to extract the hostname portion of a URL in JavaScript
Get the hostname portion via JavaScript's new URL()
function and using .hostname
:
This gets thumbs up for its brevity, support across browsers, and being regex-free.
Understanding URL structures
Before showcasing methods to extract hostnames, let's understand the URL components:
- Protocol: http or https
- Hostname: Our big fish, the domain name.
- Port: The unsung hero - an optional TCP port number.
- Pathname: The file or directory following the domain.
- Search: AKA query string, starting with
?
. - Hash: A fragment identifier beginning with
#
; likes to hang around at the end.
Familiarity with URL structure can make extraction that much smoother.
Common scenarios and solutions
Extracting current page's hostname
The current page's hostname is accessible via JavaScript's native window.location
object:
Mind you, while window.location.host
will fish out the hostname with a port if it exists, .hostname
will politely ignore the port.
Parsing a URL string
For extracting the hostname from any URL string, URL
constructor is our hero:
Regular expressions for the courageous
Regular expressions, while not as inviting or straightforward as the URL
object, are a robust tool for URL parsing:
A word of caution: with new TLDs and international domains, regular expressions could be a rough ride.
Decoding best practices
Cross-browser compatibility
Ensure your solution works smoothly across browsers. The URL
object is now widely supported, but a quick compatibility check doesn't hurt. Trust but verify, right?
Non-standard ports and you
When dealing with URLs which include non-standard ports, use the .hostname
property for a clean extraction of the hostname, no port strings attached!
Stripping unnecessary URL components
A pure hostname extraction implies removing any extraneous parts from the URL:
Dealing with non-standard URLs
For times when you encounter non-standard URLs, the URL
object constructor works with protocol-relative URLs (//www.example.com
) and URLs minus a protocol:
Was this article helpful?