Pergunta

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?

Foi útil?

Solução

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"
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top