2011-01-21 75 views
148

如何获取网址的最后一部分?我有显示锚标签的完整网址下面的脚本点击:URL的最后一部分

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

如果URL是

http://mywebsite/folder/file 

我怎么只得到它显示的URL的“文件”部分在警报框?

+0

下面是我工作的java脚本解决方案。希望它有帮助。 http://stackoverflow.com/a/38370175/610951 – 2016-07-14 09:19:33

回答

237

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

window.alert(this.href.substr(this.href.lastIndexOf('/') + 1)); 

这样一来,你会避免创建一个包含所有的URL段的数组,split()一样。

+0

如果URL中的“/”数目发生更改,分割版本是否会引起问题? – oshirowanen 2011-01-21 11:21:11

+2

@oshirowanen,不,但它会从每个网段创建一个数组元素。由于您只对最后一段感兴趣,因此分配许多永远不会使用的数组元素可能会造成浪费。 – 2011-01-21 11:23:05

+0

我在使用Chrome的初始语句时遇到了问题,下面的代码适用于我的第1行: $(window.location).attr('href') – Levi 2014-05-13 14:23:35

16

JavaScript有关联的字符串对象的功能分割,可以帮助你:

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

var lastsegment = array[array.length-1]; 
7
var urlChunks = 'mywebsite/folder/file'.split('/'); 
alert(urlChunks[urlChunks.length - 1]); 
4

此外,

var url = $(this).attr("href"); 
var part = url.substring(url.lastIndexOf('/') + 1); 
8

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

alert(href.replace(/.*\//, '')); 
3
window.alert(this.pathname.substr(this.pathname.lastIndexOf('/') + 1)); 

使用本地pathname属性,因为它是最简单的,并且已经被浏览器解析和解决。 $(this).attr("href")可以返回像../..这样的值,这不会给你正确的结果。

如果你需要保持searchhash(如foo?bar#bazhttp://quux.com/path/to/foo?bar#baz)使用此:

window.alert(this.pathname.substr(this.pathname.lastIndexOf('/') + 1) + this.search + this.hash); 
102

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

 
console.log(lastSegment);

5

我知道,这是为时已晚,但对于其他人: I 高度推荐使用PURL jquery plugin。动机PURL就是URL可以通过'#'也被分割(例如:angular.js链接),即URL可能看起来像

http://test.com/#/about/us/ 

http://test.com/#sky=blue&grass=green 

并用金银线可以轻松决定(细分/细分)您想要获得哪个细分。

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

var url = $.url('http://test.com/dir/index.html?key=value'); 
    var lastSegment = url.segment().pop(); // index.html 
5

大厦弗雷德里克的答案只使用JavaScript的:

var url = document.URL 

window.alert(url.substr(url.lastIndexOf('/') + 1)); 
3

如果您不担心使用分割产生多余的元素那么过滤器可以处理你提到的尾部斜线的问题(假设你有浏览器对过滤器的支持)。

url.split('/').filter(function (s) { return !!s }).pop() 
3
// Store original location in loc like: http://test.com/one/ (ending slash) 
var loc = location.href; 
// If the last char is a slash trim it, otherwise return the original loc 
loc = loc.lastIndexOf('/') == (loc.length -1) ? loc.substr(0,loc.length-1) : loc.substr(0,loc.lastIndexOf('/')); 
var targetValue = loc.substr(loc.lastIndexOf('/') + 1); 

targetValue =一个

如果您的网址是这样的:

http://test.com/one/

http://test.com/one

http://test.com/one/index.htm

然后禄最终看起来像:现在 http://test.com/one

,因为你想要的最后一个项目,运行下一步加载你最初想要的值(targetValue)。

var targetValue = loc.substr(loc.lastIndexOf('/') + 1); 
2
var pathname = window.location.pathname; // Returns path only 
var url  = window.location.href;  // Returns full URL 

this answer

23

只需用正则表达式的另一种解决方案复制。

var href = location.href; 
console.log(href.match(/([^\/]*)\/*$/)[1]); 
17
window.location.pathname.split("/").pop() 
1

为了让您的当前窗口的最后一段:

window.location.href.substr(window.location.href.lastIndexOf('/') +1)

0

更新raddevus答案:URL作为字符串

var loc = window.location.href; 
loc = loc.lastIndexOf('/') == loc.length - 1 ? loc.substr(0, loc.length - 1) : loc.substr(0, loc.length + 1); 
var targetValue = loc.substr(loc.lastIndexOf('/') + 1); 

最后打印路径:

test.com/path-name = path-name 

test.com/path-name/ = path-name