1

referenced from a previous SO question:

If I have http://sub.domain.com/subpage/foo/page.htm .

I use this to get the host name:

window.location.hostname : you'll get sub.domain.com

Which is all good, but what if I also want to get foo as well.

What would I use then?

Community
  • 1
  • 1
Nik
  • 334
  • 5
  • 16

1 Answers1

2

window.location.pathname seems to be what you want. For reference, you can enter window.location (or any other object) into your JavaScript console to get its properties. For this page:

{
   "ancestorOrigins":{
      "length":0
   },
   "origin":"http://stackoverflow.com",
   "hash":"#21613466",
   "search":"",
   "pathname":"/questions/21613356/how-do-i-get-the-property-that-comes-after-the-domain-name-using-javascript/21613466",
   "port":"",
   "hostname":"stackoverflow.com",
   "host":"stackoverflow.com",
   "protocol":"http:",
   "href":"http://stackoverflow.com/questions/21613356/how-do-i-get-the-property-that-comes-after-the-domain-name-using-javascript/21613466#21613466"
}
Brad
  • 159,648
  • 54
  • 349
  • 530
  • Is there any cleaner/lazy way to just get "questions" from "http://stackoverflow.com/questions/21613356/how-do-i-get-the-property-that" without getting the whole path name? – Nik Feb 06 '14 at 20:40
  • 1
    @Nik `window.location.pathname.split('/')[1]` – Brad Feb 06 '14 at 20:43