Last segment of URL

匿名 (未验证) 提交于 2019-12-03 01:57:01

问题:

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:

$(".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?

回答1:

You can also use the lastIndexOf() function to locate the last occurrence of the / character in your URL, then the substr() function to return the substring starting from that location:

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

That way, you'll avoid creating an array containing all your URL segments, as split() does.



回答2:

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


回答3:

Just another solution with regex.

var href = location.href; console.log(href.match(/([^\/]*)\/*$/)[1]); 


回答4:

window.location.pathname.split("/").pop() 


回答5:

Javascript has the function split associated to string object that can help you:

var url = "http://mywebsite/folder/file"; var array = url.split('/');  var lastsegment = array[array.length-1]; 


回答6:

Or you could use a regular expression:

alert(href.replace(/.*\//, '')); 


回答7:

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


回答8:

I know, it is too late, but for others: I highly recommended 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

    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.

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 


回答9:

Building on Frédéric's answer using only javascript:

var url = document.URL  window.alert(url.substr(url.lastIndexOf('/') + 1)); 


回答10:

Also,

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


回答11:

If you aren't worried about generating the extra elements using the split then filter could handle the issue you mention of the trailing slash (Assuming you have browser support for filter).

url.split('/').filter(function (s) { return !!s }).pop() 


回答12:

// 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 = one

If your url looks like:

http://test.com/one/

or

http://test.com/one

or

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

Then loc ends up looking like: http://test.com/one

Now, since you want the last item, run the next step to load the value (targetValue) you originally wanted.

var targetValue = loc.substr(loc.lastIndexOf('/') + 1); 


回答13:

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

Use the native pathname property because it's simplest and has already been parsed and resolved by the browser. $(this).attr("href") can return values like ../.. which would not give you the correct result.

If you need to keep the search and hash (e.g. foo?bar#baz from http://quux.com/path/to/foo?bar#baz) use this:

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


回答14:

var pathname = window.location.pathname; // Returns path only var url      = window.location.href;     // Returns full URL 

Copied from this answer



回答15:

To get the last segment of your current window:

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



回答16:

Updated raddevus answer :

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); 

Prints last path of url as string :

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


文章来源: Last segment of URL
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!