referenced from a previous SO question:
如果我有http://sub.domain.com/subpage/foo/page.htm。
我用它来获取主机名:
window.location.hostname : you'll get sub.domain.com
这一切都很好,但如果我也想获得foo
该怎么办?
那我会用什么?
答案 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"
}