How to get a subdomain using window.location?

Yes, window.location.hostname will give you subdomains as well. If this isn’t working, or isn’t supported by some other browser, you could quite easily parse for it:

// window.location.href == "http://sample.somedomain.com/somedir/somepage.html"
var domain = /:\/\/([^\/]+)/.exec(window.location.href)[1];

Leave a Comment

tech