我需要在JS/jQuery中获取路径名(www.my-site.com/this-part/and-this-part/etc/),但是我需要它作为字符串而不是对象。
换句话说,我需要在JS/jQuery中获取$_SERVER['REQUEST_URI'];
的值。
我已经尝试过:
var page_pathname = location.pathname;
var page_pathname = location.pathname + location.search;
var page_pathname = (location.pathname+location.search).substr(1);
console.log
返回的结果:
1. Object {error: Object}
2. Location {hash: "", search: "", pathname: "/my-cat/my-title/", port: "", hostname: "www.my-site.com"…}
我需要的是 my-cat/my-title/
,可以通过以下方式得到:
location.pathname
返回一个字符串,而不是一个对象。console.log(location.pathname.substr(1))
应该适用于你。 - Rory McCrossan.pathname
始终是一个字符串。 - Alnitak