Is there a really easy way to start from a full URL:
document.location.href = "http://aaa.bbb.ccc.com/asdf/asdf/sadf.aspx?blah"
And extract just the host part:
aaa.bbb.ccc.com
There's gotta be a JavaScript function that does this reliably, but I can't find it.
The getHost() method of URL class returns the hostname of the URL. This method will return the IPv6 address enclosed in square brackets ('['and']').
If you only want to return the hostname value (excluding the port number), use the window. location. hostname method instead. This will return a string value containing the hostname and, if the port value is non-empty, a : symbol along with the port number of the URL.
hostname property returns the host (IP adress or domain) of a URL. The location. hostname property can also be set, to navigate to the same URL with a new hostname.
The Location Host property in HTML is used to sets or returns the hostname and port of a URL. The Location Hash property doesn't return the port number if it is not specified in the URL. Syntax: It returns the host property.
suppose that you have a page with this address: http://sub.domain.com/virtualPath/page.htm
. use the following in page code to achive those results:
window.location.host
: you'll get sub.domain.com:8080
or sub.domain.com:80
window.location.hostname
: you'll get sub.domain.com
window.location.protocol
: you'll get http:
window.location.port
: you'll get 8080
or 80
window.location.pathname
: you'll get /virtualPath
window.location.origin
: you'll get http://sub.domain.com
*****Update: about the .origin
***** As the ref states, browser compatibility for window.location.origin
is not clear. I've checked it in chrome and it returned http://sub.domain.com:port
if the port is anything but 80, and http://sub.domain.com
if the port is 80.
Special thanks to @torazaburo for mentioning that to me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With