如何使用JavaScript获得域名之后的属性



从以前的问题引用:

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

我用它来获取主机名:

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

最新更新