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

时间:2014-02-06 20:28:43

标签: javascript jquery localhost hostname

referenced from a previous SO question:

如果我有http://sub.domain.com/subpage/foo/page.htm

我用它来获取主机名:

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

这一切都很好,但如果我也想获得foo该怎么办?

那我会用什么?

1 个答案:

答案 0 :(得分:2)

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