If the value of window.location.hostname
gives something like "example.com", How can I get just "example" out of the result?
How can I make window.location.hostname
return without .com or .org
If the value of window.location.hostname
gives something like "example.com", How can I get just "example" out of the result?
How can I make window.location.hostname
return without .com or .org
Assuming it's a top level domain(and not, mail.somedomain.com), the following code would give you "somedomain" instead of "somedomain.com".
var basehost = window.location.hostname.split(".")[0];
You can't make window.location.hostname
'return' anything different as you're using the inbuilt API that your browser provides.
What you can do instead is use some creative use of String.prototype.split
or a regex to manipulate the string value you get from that property.
Remember to consider cases like .co.uk
where there is a dot in the middle of the TLD.