Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
60
rated 0 times [  66] [ 6]  / answers: 1 / hits: 53073  / 15 Years ago, wed, july 29, 2009, 12:00:00

If I have a hostname such as: http://sample.example.com and in Javascript I do window.location.hostname, would I get example.com or sample.example.com?



If not, how would I be able to get sample.example.com?


More From » subdomain

 Answers
6

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];

[#99034] Friday, July 24, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alejandro

Total Points: 231
Total Questions: 102
Total Answers: 107

Location: Jordan
Member since Wed, Jun 17, 2020
4 Years ago
alejandro questions
Mon, Jul 18, 22, 00:00, 2 Years ago
Fri, Sep 18, 20, 00:00, 4 Years ago
Thu, Sep 10, 20, 00:00, 4 Years ago
;