Javascript URL的最后一段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4758103/
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
Last segment of URL
提问by oshirowanen
How do I get the last segment of a url? I have the following script which displays the full url of the anchor tag clicked:
如何获取网址的最后一段?我有以下脚本显示单击的锚标记的完整 url:
$(".tag_name_goes_here").live('click', function(event)
{
event.preventDefault();
alert($(this).attr("href"));
});
If the url is
如果网址是
http://mywebsite/folder/file
how do I only get it to display the "file" part of the url in the alert box?
我如何才能让它在警告框中显示 url 的“文件”部分?
回答by Frédéric Hamidi
You can also use the lastIndexOf()function to locate the last occurrence of the /
character in your URL, then the substring()function to return the substring starting from that location:
您还可以使用lastIndexOf()函数定位/
URL 中最后一次出现的字符,然后使用substring()函数返回从该位置开始的子字符串:
console.log(this.href.substring(this.href.lastIndexOf('/') + 1));
That way, you'll avoid creating an array containing all your URL segments, as split()
does.
这样,您将避免创建一个包含所有 URL 段的数组,就像split()
这样。
回答by Tim van Oostrom
var parts = 'http://mywebsite/folder/file'.split('/');
var lastSegment = parts.pop() || parts.pop(); // handle potential trailing slash
console.log(lastSegment);
回答by Dblock247
window.location.pathname.split("/").pop()
回答by Avirtum
Just another solution with regex.
正则表达式的另一种解决方案。
var href = location.href;
console.log(href.match(/([^\/]*)\/*$/)[1]);
回答by Fran Verona
Javascript has the function split associated to string object that can help you:
Javascript 具有与字符串对象相关联的函数 split 可以帮助您:
var url = "http://mywebsite/folder/file";
var array = url.split('/');
var lastsegment = array[array.length-1];
回答by Sebastian Barth
The other answers may work if the path is simple, consisting only of simple path elements. But when it contains query params as well, they break.
如果路径很简单,仅由简单的路径元素组成,则其他答案可能有效。但是当它也包含查询参数时,它们就会中断。
Better use URLobject for this instead to get a more robust solution. It is a parsed interpretation of the present URL:
最好为此使用URL对象,以获得更强大的解决方案。它是对当前 URL 的解析解释:
Input:const href = 'https://stackoverflow.com/boo?q=foo&s=bar'
输入:const href = 'https://stackoverflow.com/boo?q=foo&s=bar'
const segments = new URL(href).pathname.split('/');
const last = segments.pop() || segments.pop(); // Handle potential trailing slash
console.log(last);
Output:'boo'
输出:'boo'
This works for all common browsers. Only our dying IE doesn't support that (and won't). For IE there is a polyfillsavailable, though (if you careat all).
这适用于所有常见的浏览器。只有我们垂死的 IE 不支持(也不会)。不过,对于 IE,有一个polyfills可用(如果你在乎的话)。
回答by jasssonpet
Or you could use a regular expression:
或者您可以使用正则表达式:
alert(href.replace(/.*\//, ''));
回答by acme
var urlChunks = 'mywebsite/folder/file'.split('/');
alert(urlChunks[urlChunks.length - 1]);
回答by IL55
I know, it is too late, but for others: I highlyrecommended use PURL jquery plugin. Motivation for PURL is that url can be segmented by '#'too (example: angular.js links), i.e. url could looks like
我知道,为时已晚,但对于其他人:我强烈建议使用PURL jquery plugin。PURL 的动机是 url 也可以用“#”分割(例如:angular.js 链接),即 url 看起来像
http://test.com/#/about/us/
or
或者
http://test.com/#sky=blue&grass=green
And with PURL you can easy decide (segment/fsegment) which segment you want to get.
使用 PURL,您可以轻松决定 (segment/fsegment) 您想要获得哪个段。
For "classic" last segment you could write:
对于“经典”最后一段,你可以写:
var url = $.url('http://test.com/dir/index.html?key=value');
var lastSegment = url.segment().pop(); // index.html
回答by Pinch
Building on Frédéric's answer using only javascript:
仅使用 javascript 以 Frédéric 的回答为基础:
var url = document.URL
window.alert(url.substr(url.lastIndexOf('/') + 1));