문제

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?

도움이 되었습니까?

해결책

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"
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top