Get last part of url using jQuery

If the "/" is definitely at the end of your url, no matter what.. then just change:

var last_part = parts[parts.length-2]

(use 2 instead of 1) because of ncdreamy's comment.

Also, you don't need var var var var var... just var once and a comma separator:

var url = $(location).attr('href'),
    parts = url.split("/"),
    last_part = parts[parts.length-2];

You can use:

hrefurl=$(location).attr("href");
last_part=hrefurl.substr(hrefurl.lastIndexOf('/') + 1)

using jquery

$(location).attr("href").split('/').pop();

Tags:

Jquery