最后一段网址

osh*_*nen 213 javascript jquery

如何获取网址的最后一段?我有以下脚本显示单击的锚标记的完整URL:

$(".tag_name_goes_here").live('click', function(event)
{
    event.preventDefault();  
    alert($(this).attr("href"));
});
Run Code Online (Sandbox Code Playgroud)

如果网址是

http://mywebsite/folder/file
Run Code Online (Sandbox Code Playgroud)

我如何才能在警告框中显示网址的"文件"部分?

Fré*_*idi 336

您还可以使用lastIndexOf()函数来查找/URL中最后一次出现的字符,然后使用substr()函数返回从该位置开始的子字符串:

console.log(this.href.substring(this.href.lastIndexOf('/') + 1));
Run Code Online (Sandbox Code Playgroud)

这样,您就可以避免创建包含所有网段的数组split().

  • 但是如果网址变得像`admin/store/tour/-1 /`那么它将是'''`字符串? (9认同)
  • Attn:@Set Kyar Wa Lar Aug和@Sнаđошƒаӽ解决方案的url,最后包含一个`/`:`var url = window.location.href.replace(/\/ $ /,'');/*删除可选的结尾/*/var lastSeg = url.substr(url.lastIndexOf('/')+ 1);` (6认同)
  • @oshirowanen,不,但它会从每个URL段创建一个数组元素.由于您只对最后一个段感兴趣,因此分配许多您永远不会使用的数组元素可能会很浪费. (3认同)
  • 如果 url 末尾包含`/`怎么办? (3认同)

小智 138

var parts = 'http://mywebsite/folder/file'.split('/');
var lastSegment = parts.pop() || parts.pop();  // handle potential trailing slash

console.log(lastSegment);
Run Code Online (Sandbox Code Playgroud)

  • 如果url以"/"结尾,即``http:// mywebsite/folder/file /``` (10认同)
  • 旁注:如果 URL 确实设置了查询参数(例如 `.../file?var=value&foo=bar`),则此方法不起作用。该解决方案以更强大和现代的方式解决了这个问题:/sf/answers/3625658571/ (4认同)

Dbl*_*247 67

window.location.pathname.split("/").pop()
Run Code Online (Sandbox Code Playgroud)

  • 简单容易。谢谢。 (2认同)
  • 完美整洁,很棒 (2认同)
  • 美丽干净的回应 (2认同)
  • @SebastianBarth 这不是在寻找查询字符串参数。对 .pathname 的调用会将它们删除,因为重点是获取 url 路径的最后一段,无论是否有查询参数。 (2认同)

Avi*_*tum 27

只是另一种正则表达式的解决方案.

var href = location.href;
console.log(href.match(/([^\/]*)\/*$/)[1]);
Run Code Online (Sandbox Code Playgroud)

  • 如果URL以/结尾,这很有效 (2认同)

Fra*_*ona 18

Javascript具有与字符串对象相关联的函数split,可以帮助您:

var url = "http://mywebsite/folder/file";
var array = url.split('/');

var lastsegment = array[array.length-1];
Run Code Online (Sandbox Code Playgroud)


小智 17

如何使用,和获取URL最后一段的最短方法split()filter()pop()

function getLastUrlSegment(url) {
  return new URL(url).pathname.split('/').filter(Boolean).pop();
}

console.log(getLastUrlSegment(window.location.href));
console.log(getLastUrlSegment('https://x.com/boo'));
console.log(getLastUrlSegment('https://x.com/boo/'));
console.log(getLastUrlSegment('https://x.com/boo?q=foo&s=bar=aaa'));
console.log(getLastUrlSegment('https://x.com/boo?q=foo#this'));
console.log(getLastUrlSegment('https://x.com/last segment with spaces'));
Run Code Online (Sandbox Code Playgroud)

对我有用。


Seb*_*rth 11

如果路径很简单(仅由简单路径元素组成),则其他答案可能会起作用。但是,当它还包含查询参数时,它们就会中断。

最好为此使用URL对象,以获得更可靠的解决方案。它是对当前URL的解析解释:

输入: const href = 'https://stackoverflow.com/boo?q=foo&s=bar'

const last = new URL(href).pathname.split('/').pop();
console.log(last);
Run Code Online (Sandbox Code Playgroud)

输出: 'boo'

这适用于所有常见的浏览器。只有我们垂死的IE不支持(并且不会)。对于IE,可以使用polyfills(如果您很在意的话)。


jas*_*pet 9

或者您可以使用正则表达式:

alert(href.replace(/.*\//, ''));
Run Code Online (Sandbox Code Playgroud)


acm*_*cme 8

var urlChunks = 'mywebsite/folder/file'.split('/');
alert(urlChunks[urlChunks.length - 1]);
Run Code Online (Sandbox Code Playgroud)


Joh*_*rty 7

返回最后一段,无论尾部斜杠如何:

var val = 'http://mywebsite/folder/file//'.split('/').filter(Boolean).pop();

console.log(val);
Run Code Online (Sandbox Code Playgroud)

  • 好主意,但它不再在浏览器中工作 (2认同)

IL5*_*L55 5

我知道,为时已晚,但对于其他人:我强烈建议使用PURL jquery插件.PURL的动机是url也可以用'#'来分段(例如:angular.js链接),即url看起来像

    http://test.com/#/about/us/
Run Code Online (Sandbox Code Playgroud)

要么

    http://test.com/#sky=blue&grass=green
Run Code Online (Sandbox Code Playgroud)

使用PURL,您可以轻松决定(细分/细分)您想要获得的细分.

对于"经典"的最后一段你可以写:

    var url = $.url('http://test.com/dir/index.html?key=value');
    var lastSegment = url.segment().pop(); // index.html
Run Code Online (Sandbox Code Playgroud)