如何使用javascript获取此url的最后一个单词(“WW”)

如何使用javascript获取此url的最后一个单词(“WW”),javascript,url,Javascript,Url,url为' 如何得到最后一个字 谢谢 你可能需要提供更多关于你想要发生什么的信息。是否要检索第三个目录?还是最后一个目录?等等。在你提供给我之前,我的答案是有效的 当作者给出URL并询问如何获取“WW”时,就提供了上述答案,而没有其他问题。该死的宽限期 var path = location.pathname; var directories = path.split("/"); var lastDirecotry = directories[(directories.length - 1)];

url为'

如何得到最后一个字

谢谢

你可能需要提供更多关于你想要发生什么的信息。是否要检索第三个目录?还是最后一个目录?等等。在你提供给我之前,我的答案是有效的

当作者给出URL并询问如何获取“WW”时,就提供了上述答案,而没有其他问题。该死的宽限期

var path = location.pathname;
var directories = path.split("/");
var lastDirecotry = directories[(directories.length - 1)];
还没有测试,可能需要一些修改,但你明白了

你可能需要提供更多关于你想要发生什么的信息。是否要检索第三个目录?还是最后一个目录?等等。在你提供给我之前,我的答案是有效的

当作者给出URL并询问如何获取“WW”时,就提供了上述答案,而没有其他问题。该死的宽限期

var path = location.pathname;
var directories = path.split("/");
var lastDirecotry = directories[(directories.length - 1)];

还没有测试过,可能需要一些修改,但你已经明白了。

如果你在一个有此url的页面上

var hrefParts = location.href.split('/');
hrefParts[hrefParts.length - 1] == "wwww";
否则,

var href = "http://127.0.0.1:8000/maps/map/wwww/";
href.split('/')[href.length - 1] == "wwww" // you get the idea...

如果您位于具有此url的页面上

var hrefParts = location.href.split('/');
hrefParts[hrefParts.length - 1] == "wwww";
否则,

var href = "http://127.0.0.1:8000/maps/map/wwww/";
href.split('/')[href.length - 1] == "wwww" // you get the idea...

您可以使用window.location.pathname并拆分字符串

大概是这样的:

var pathArray = window.location.pathname.split( '/' );
alert(pathArray[pathArray.length-1]);

您可以使用window.location.pathname并拆分字符串

大概是这样的:

var pathArray = window.location.pathname.split( '/' );
alert(pathArray[pathArray.length-1]);

+1尽管hrefParts[hrefParts.lenth-1]对于最后一个字是最好的,而不是硬编码的5。+1尽管hrefParts[hrefParts.lenth-1]对于最后一个字是最好的,而不是硬编码的5.var path=location.pathname var directories=path.split/;path=目录[directories.length-2];var path=location.pathname var directories=path.split/;path=目录[directories.length-2];