location.href
返回的是这样的:
http://stackoverflow.com/questions/ask
但我只想获取questions/ask
(第一个字符没有/
)
如何实现?
location.href
返回的是这样的:
http://stackoverflow.com/questions/ask
但我只想获取questions/ask
(第一个字符没有/
)
如何实现?
location.pathname.substr(1)
是什么意思。
位置(location
)对象具有pathname
属性。
这将给出/questions/ask
,要删除第一个字符,请使用substring(1)
:
var path = location.pathname.substring(1);
substr
还是substring
? - wampsubstr
函数将长度作为第二个参数,而substring
函数则使用另一个索引(提取字符串的上限)。在这种情况下,无论您使用哪个函数都没有关系(我假设内部实际上是相同的函数)。 - Felix Klinglocation.pathname.substring(1)
。var uri = window.location.href.substr(window.location.protocol.length + window.location.hostname.length + 2);
var path = (window.location.pathname+window.location.search).substr(1);
var path = (window.location.href.replace(window.location.origin, '')).substr(1);
substr
有一个可选的长度参数,而substring
则有一个可选的结束索引参数。在这种情况下,如果只想要从起始位置开始的所有内容,它们将执行相同的操作。 - iamfacehref
字符串变量代替location.pathname
? - Dimitri Kopriwa