11

I am trying to grab the last part of the current url:

URL: http://example.com/test/action

I am trying to grab "action".

The URL is always consistent in that structure. But it may have some extra params at the end.

This is in a rails app using the prototype framework. In rails it would be params[:id].

Rob W
  • 341,306
  • 83
  • 791
  • 678
kush
  • 16,408
  • 17
  • 48
  • 65

5 Answers5

37

You could simply use .split() on window.location.href, and grab the last entry.

Example:

var lastPart = window.location.href.split("/").pop();
Sampson
  • 265,109
  • 74
  • 539
  • 565
  • Good suggestion, Chetan. Updated my response. – Sampson Dec 02 '09 at 19:00
  • This will create an array (the split method will) while Jacobs's solution will not. – Rafael Herscovici Oct 05 '12 at 15:58
  • If you not interested in everything that comes after the path (url params), use `window.location.pathname` instead of `href`. – Patrick Hammer May 10 '15 at 10:09
  • It's so strange, I just can't get this to work. So your code creates an array, but I just want a string. So I converted the array to a string with .toString(), and tried displaying it using alert(). It just gives me an empty alert. – Jonathan Neeskens Apr 19 '17 at 11:43
  • 2
    @JonathanNeeskens The `.split` method creates the array. The `.pop` method returns the last item _in the array_, which should be a string. If your array was empty, you likely had no value to begin with, or a bad reference. Run `"foo".split("/")` and you'll get `["foo"]`. Run `"foo/bar".split("/")` and you'll get `["foo", "bar"]`. `["foo", "bar"].pop()` wil return `"bar"`. – Sampson Apr 21 '17 at 18:24
6

The other answers are fine, however if your url looks like:

http://example.com/test/action?foo=bar

they will fail to give you just "action". To do this you'd use pathname, which contains only the path information exclusive of query string parameters (ie /test/action in this case):

location.pathname.split('/').pop();
Roatin Marth
  • 23,589
  • 3
  • 51
  • 55
4

use document.location.href.substring( document.location.href.lastIndexOf( '/' ) );

Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
  • 3
    I'd go with `location.pathname` instead of `location.href` if you don't want to grab the query part of the URL; also, MDC recommends to use `window.location` instead of `document.location` for compatibility reasons – Christoph Dec 02 '09 at 18:58
  • 1
    This will fail to get the fragment if the url ends with `/`, but it does answer the OP's answer perfectly. – Rafael Herscovici Oct 05 '12 at 16:00
0

with jquery :

var idFromUrl = window.location.href.split("/").pop();
$('#various3').attr('href', $('#various3').attr('href').replace('placed_id', idFromUrl)); 
Bruno
  • 4,685
  • 7
  • 54
  • 105
0

Below is my working javascript solution.Hope it helps some one and hence posting.This solution is to retrieve last part of url leaving the generally not needed query string part.It helps to identify from which page the visit happened and write logic based on that.

 var url = document.referrer;
    var start = url.lastIndexOf("/") + 1;
    var len = url.indexOf("?");
    if (len > 0)
        url = url.substring(start, len);
    else
        url = url.substring(start);