如何获取url的最后一部分?我有以下脚本,它会显示单击的锚点标签的完整url:
$(".tag_name_goes_here").live('click', function(event)
{
event.preventDefault();
alert($(this).attr("href"));
});
如果URL是
http://mywebsite/folder/file
如何仅在警告框中显示URL的“file”部分?
如何获取url的最后一部分?我有以下脚本,它会显示单击的锚点标签的完整url:
$(".tag_name_goes_here").live('click', function(event)
{
event.preventDefault();
alert($(this).attr("href"));
});
如果URL是
http://mywebsite/folder/file
如何仅在警告框中显示URL的“file”部分?
/
字符的位置,然后使用 substring() 函数从该位置开始返回子字符串:console.log(this.href.substring(this.href.lastIndexOf('/') + 1));
这样,您将避免创建包含所有URL段的数组,就像split()
函数一样。
admin/store/tour/-1/
,那么它会变成一个空字符串''
吗? - Set Kyar Wa Larhref
属性,它是未解析的,可能包含像../
这样的东西,表示上一级目录。相反,应该使用本地的属性,例如this.href
。 - Walfvar url = window.location.href.replace(/\/$/, ''); /* 删除可选的结尾/ */ var lastSeg = url.substr(url.lastIndexOf('/') + 1);
- Rossvar parts = 'http://mywebsite/folder/file'.split('/');
var lastSegment = parts.pop() || parts.pop(); // handle potential trailing slash
console.log(lastSegment);
.../file?var=value&foo=bar
),此解决方案将无法起作用。下面这个方案是更加健壮和现代的解决方法:https://dev59.com/u2445IYBdhLWcg3wpb4P#51795122 - Sebastian Barthvar lastSegment = parts.pop().split('?')[0] || parts.pop();
- Moriwindow.location.pathname.split("/").pop()
console.log(history.location.pathname.split("/").pop())
如果有更好的方法,我想知道!希望这个评论可以让更多的人看到你的答案。谢谢@Dblock247 - tralawarconst 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);
输出:'boo'
这适用于所有常见的浏览器。只有我们即将消失的IE不支持此功能(也不会支持)。但是,对于IE,有一个polyfills可用(如果您在意的话)。
segments
将是所有路径的数组,所以如果你有一个 URL,比如:https://a.com/b/c/d/?letters=true
,那么在这个例子中 segments
等于 ['b', 'c', 'd', '']
,因为 d
后面的斜杠导致出现空字符串 ''
。然后 last
变量表示“获取 segments
数组中的最后一项并将其删除。如果该项为假,则再次执行相同的操作”(逻辑或),这将导致在我的示例中返回 d
。 - maxshuty只是另一种使用正则表达式的解决方案。
var href = location.href;
console.log(href.match(/([^\/]*)\/*$/)[1]);
Javascript有一个与字符串对象相关联的split函数,可以帮助你:
const url = "http://mywebsite/folder/file";
const array = url.split('/');
const lastsegment = array[array.length-1];
使用 split()
、filter()
和 pop()
获取 URL 最后一段的最短方法。
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'));
对我而言是有效的。
或者您可以使用正则表达式:
alert(href.replace(/.*\//, ''));
alert(window.location.href.replace(/\/$/, '').replace(/.*\//, ''))
- samsonvar urlChunks = 'mywebsite/folder/file'.split('/');
alert(urlChunks[urlChunks.length - 1]);
var val = 'http://mywebsite/folder/file//'.split('/').filter(Boolean).pop();
console.log(val);
path.sep
而不是一个字面上的斜杠。请参阅 https://nodejs.org/api/path.html#path_path_sep。这将使你的代码在不同操作系统之间具有可移植性。 - Aaron
/
分割href
。一个 URL 包括路径之后的部分:查询参数(在?
后面)和片段(在#
后面)。如果您将 URL 作为字符串处理,请先使用new
URL
(
字符串).pathname
。如果您正在查找当前页面的 URL,请使用location.pathname
。然后 在结果上使用.split("/")
。.filter(Boolean)
可去除空部分(比如尾斜杠后面的部分)。.at(-1)
(或.pop()
或.slice(-1)[0]
)可获取路径的最后一部分。 - Sebastian Simon