views:

71

answers:

6

I've got a url like this:

http://www.site.com/234234234

I need to grab the Id after /, so in this case 234234234

How can i do this easily?

+2  A: 
var url = "http://www.site.com/234234234"
var stuff = url.split('/');
var id = stuff[stuff.length-1];
//id = 234234234
John Strickler
+3  A: 

Get a substring after the last index of /.

var url = 'http://www.site.com/234234234';
var id = url.substring(url.lastIndexOf('/') + 1);
alert(id); // 234234234

It's just basic JavaScript, no jQuery involved.

BalusC
+1  A: 

Using the jQuery URL Parser plugin, you should be able to do this:

jQuery.url.segment(1)
Jeff
I like this one because most other of the proposed solutions seem to fail when there are anchors, query parameters, ... involved.
Jonas Wagner
A: 
var url = window.location.pathname;
var id = url.substring(url.lastIndexOf('/') + 1);
JungleFreak
A: 
var full_url = document.URL; // Get current url
var url_array = full_url.split('/') // Split the string into an array with / as separator
var last_segment = url_array[url_array.length-1];  // Get the last part of the array (-1)
alert( last_segment ); // Alert last segment
Fred Bergman
+1  A: 

Just because I can:

function pathName(url, a) {
   return (a = document.createElement('a'), a.href = url, a.pathname); //optionally, remove leading '/'
}

pathName("http://www.site.com/234234234") -> "/234234234"
CD Sanchez