analizar URL con javascript o jquery

I have a url in this form http://www.example.com/data/45. How do I get the last element (45 in this example) using jQuery or JavaScript?

Gracias.

preguntado el 01 de febrero de 12 a las 03:02

3 Respuestas

A very quick plain JavaScript method, assuming it's the URL of the current page (in window.location.href)

var urlNum = window.location.href.split('/').pop();

.split('/') obviously chops the URL up into an array, based on divisions by /. Since you only need the last part, it is probably the easiest method.

.pop() removes the last element from the array returned by .split() y lo devuelve.

Respondido 01 Feb 12, 07:02

Thanks! I will try this out later - revólver

var num = 'http://www.example.com/data/45'.split('/').pop(); //num now == '45'

Respondido 01 Feb 12, 07:02

My rendition :)

var url         = 'http://www.example.com/data/45';
var lastSegment = url.substr(url.lastIndexOf('/') + 1);

Respondido 01 Feb 12, 07:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.