2011-01-21 68 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