Javascript 如何在javascript中从location.href获取请求uri?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3263303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get request uri from location.href in javascript?
提问by wamp
What I get from location.hrefis like this:
我得到的location.href是这样的:
http://stackoverflow.com/questions/ask
But I only want to get questions/ask(no /at the first character)
但我只想得到questions/ask(没有/第一个字符)
How to achieve this?
如何实现这一目标?
回答by Marcel Hymanwerth
location.pathname.substr(1)would that be.
location.pathname.substr(1)那会是。
回答by Felix Kling
回答by Gunjan
You can use location.pathname.substring(1)
您可以使用 location.pathname.substring(1)
回答by AssassiN
var uri = window.location.href.substr(window.location.protocol.length + window.location.hostname.length + 2);
This code also includes GET and HASHTAGS (basically everything after hostname)
此代码还包括 GET 和 HASHTAGS(基本上是主机名之后的所有内容)

