如何使用javascript获取域名后面的属性?

如何使用javascript获取域名后面的属性?,javascript,jquery,localhost,hostname,Javascript,Jquery,Localhost,Hostname,如果我有 我使用它来获取主机名: window.location.hostname : you'll get sub.domain.com 这很好,但是如果我也想得到foo呢 那么我将使用什么呢?window.location.pathname似乎就是您想要的。作为参考,您可以在JavaScript控制台中输入window.location(或任何其他对象)以获取其属性。对于本页: { "ancestorOrigins":{ "length":0 }, "orig

如果我有

我使用它来获取主机名:

window.location.hostname : you'll get sub.domain.com
这很好,但是如果我也想得到
foo


那么我将使用什么呢?

window.location.pathname
似乎就是您想要的。作为参考,您可以在JavaScript控制台中输入
window.location
(或任何其他对象)以获取其属性。对于本页:

{
   "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"
}

有没有更干净/更懒惰的方法只从“”获取“问题”,而不获取整个路径名?@Nik
window.location.pathname.split('/')[1]