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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:56:25  来源:igfitidea点击:

How to get request uri from location.href in javascript?

javascriptdom

提问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

The locationobject has a pathnameproperty.

location对象有一个pathname属性。

This will give you /questions/askand to remove the first character, use substring(1):

这将为您提供/questions/ask并删除第一个字符,请使用substring(1)

var path = location.pathname.substring(1);

回答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(基本上是主机名之后的所有内容)