简体   繁体   English

JavaScript,如何获取URL路径?

[英]javascript, how to get url path?

I wanna get url path by javascript, 我想通过javascript获取网址路径,
for example, i have these urls: 例如,我有这些网址:

http://localhost:8080/blah/blah/index.php?p=1

http://localhost:8080/blah/blah/index.php

http://localhost:8080/blah/blah/

http://localhost:8080/blah/blah

and i would like to get: http://localhost:8080/blah/blah/ anyone please help me 我想得到: http://localhost:8080/blah/blah/任何人都可以帮助我

This appears to be an ambigious problem with no definitive solution because there is no concrete definition of what you want removed from the end of the URL. 这似乎是一个模棱两可的问题,没有确定的解决方案,因为没有具体定义要从URL末尾删除的内容。

It would be easy to remove the query parameters (first example). 删除查询参数很容易(第一个示例)。

It would be easy to normalize the trailing slash (last two examples). 标准化尾随斜杠很容易(最后两个示例)。

It would be easy to remove the last piece of the path if that is what you always wanted, but unless you're willing to add some more rules that can distinguish /index.php from /blah at the end of the URL so that an algorithm knows when to remove one, but not the other, what you have asked for is not possible. 如果这是您一直想要的,则删除路径的最后一部分很容易,但是除非您愿意在URL的末尾添加更多可以将/index.php/blah区分开的规则,否则该算法知道何时删除其中一个,而不能删除另一个,因此您所要求的是不可能的。 As you have defined the problem, the code cannot know whether the blah at the end of http://localhost:8080/blah/blah should be removed or not. 正如你所定义的问题,代码就可以知道是不是blah ,在结束http://localhost:8080/blah/blah应该被删除或没有。

For example, this code will remove the query parameters and the last piece of the URL, but that will only work on the three of the four examples, but runs into the ambiguity about what you want removed at the end: 例如,此代码将删除查询参数和URL的最后一部分,但这仅适用于四个示例中的三个,但最后却模糊了您想要删除的内容:

function cleanURL(url) {
    return(url.replace(/\?.*$/, "").replace(/\/[^\/]*$/, "") + "/");
}

And here it is being run on all four of your examples: http://jsfiddle.net/jfriend00/2Q4Ue/ . 在这里,它正在您的所有四个示例上运行: http : //jsfiddle.net/jfriend00/2Q4Ue/ It returns these results for the four cases: 它针对以下四种情况返回这些结果:

http://localhost:8080/blah/blah/
http://localhost:8080/blah/blah/
http://localhost:8080/blah/blah/
http://localhost:8080/blah/

For the last URL, you can see that the algorithm identified /blah at the end as the filename and removed it since there is no way to know if that's part of the path or a filename. 对于最后一个URL,您可以看到该算法在末尾将/blah标识为文件名并删除了它,因为无法知道该文件是路径还是文件名的一部分。

If you were willing to make a new rule that the filename must have a file extension and the last piece of the pathname when there is no filename will never have an extension, then such an algorithm could be written to handle the last case. 如果您愿意制定一个新规则,即文件名必须具有文件扩展名,而没有文件名时路径名的最后一部分将永远不具有扩展名,那么可以编写这样的算法来处理最后一种情况。

OK, with a new rule that a filename at the end is anything with a period in it, then, you can use this: 好的,使用一条新规则,即文件名末尾是带有句点的任何内容,那么您可以使用以下命令:

function cleanURL(url) {
    return(url.replace(/\?.*$/, "")
           .replace(/\/[^\/]*\.[^\/]*$/, "")
           .replace(/\/$/, "") + "/");
}

Which is shown in this fiddle: http://jsfiddle.net/jfriend00/LpauM/ and produces this result for the four test URLs: 该小提琴中显示了这一点: http : //jsfiddle.net/jfriend00/LpauM/并为四个测试URL生成此结果:

http://localhost:8080/blah/blah/
http://localhost:8080/blah/blah/
http://localhost:8080/blah/blah/
http://localhost:8080/blah/blah/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM