47

I've got a URL like this:

http://www.foo.bar/234234234

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

How can I do this easily?

Stphane
  • 3,368
  • 5
  • 32
  • 47
wesbos
  • 25,839
  • 30
  • 106
  • 143

14 Answers14

72

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,082,665
  • 372
  • 3,610
  • 3,555
  • 4
    This works great! But what if there is url-stuff on the right side of the url? – Tom Jan 25 '16 at 17:49
  • I don't think that this answer cover all possible cases. Because as Tom mentioned above, there could be situation the second part will have some hash, or parameters and then you get what you don't want. E.g. "http://www.example.com/234234234?lang=en" or "http://www.example.com/234234234#tab-id" etc.. – Lubos K. Aug 02 '21 at 13:12
  • @LubosK: I was just answering within the context of the question. Simply ask/search a new question if you want a more tailored answer to it. Or simply research how exactly to split an URL on `?` and `#` (and `;`) characters and then take the 1st part of it. You don't know if the OP already did this beforehand and omitted it for brevity. – BalusC Aug 02 '21 at 13:14
  • @BalusC: okay, sure, I fully agree with you. But I just wanted to point out of possible failures/cases for future readers. – Lubos K. Aug 02 '21 at 21:03
58
var url = window.location.pathname;
var id = url.substring(url.lastIndexOf('/') + 1);
Rohrbs
  • 1,855
  • 13
  • 11
15
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,949
  • 5
  • 22
  • 30
8
var url = "http://www.site.com/234234234"
var stuff = url.split('/');
var id = stuff[stuff.length-1];
//id = 234234234
John Strickler
  • 25,151
  • 4
  • 52
  • 68
6
const url = "http://www.example.com/1234"
const id = url.split('/').pop();

Try this, it is much easier

The output gives 1234

Mark Kelly
  • 1,780
  • 7
  • 16
RAVI singh
  • 158
  • 2
  • 15
5

You could just use window.location.hash to grab the id.

var id = window.location.hash;

I don't think you will need that much code to achieve this.

Simon Gomes
  • 383
  • 1
  • 5
  • 18
4

try this javascript

Snippet for getting the parameters from URL. Use javascript to get the URL parameters either from current window location or static URL into the argument for the function call.

javascript

function getUrlParameters(parameter, staticURL, decode){

       var currLocation = (staticURL.length)? staticURL : window.location.search,
           parArr = currLocation.split("?")[1].split("&"),
           returnBool = true;

       for(var i = 0; i < parArr.length; i++){
            parr = parArr[i].split("=");
            if(parr[0] == parameter){
                return (decode) ? decodeURIComponent(parr[1]) : parr[1];
                returnBool = true;
            }else{
                returnBool = false;            
            }
       }

       if(!returnBool) return false;  
    }

To get the parameter “id” from above static URL, use the following:

var idParameter = getUrlParameters("id", "http://www.example.com?id=1234&auth=true", true);

or

var idParameter = getUrlParameters("id", "", true);
Boopathi Rajan
  • 1,212
  • 15
  • 38
4

My url is like this http://www.default-search.net/?sid=503 . I want to get 503 . I wrote the following code .

var baseUrl = (window.location).href; // You can also use document.URL
var koopId = baseUrl.substring(baseUrl.lastIndexOf('=') + 1);
alert(koopId)//503

If you use

var v = window.location.pathname;
console.log(v)

You will get only "/";

sourcecode
  • 4,116
  • 1
  • 20
  • 14
1

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

jQuery.url.segment(1)
Jeff
  • 21,744
  • 6
  • 51
  • 55
  • I like this one because most other of the proposed solutions seem to fail when there are anchors, query parameters, ... involved. – Jonas Wagner Sep 16 '10 at 20:13
1

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"
Cristian Sanchez
  • 31,171
  • 11
  • 57
  • 63
1

Yet another option using the built-in URL interface which worth it when one has more URL specific work to do besides string extraction.

The URL interface is used to parse, construct, normalize, and encode URLs. It works by providing properties which allow you to easily read and modify the components of a URL.

const url = new URL('http://www.foo.bar/234234234');
alert(url.pathname.slice(1)); // 234234234
Stphane
  • 3,368
  • 5
  • 32
  • 47
0

Try this

var url = "http://www.exmple.com/234234234"
var res = url.split("/").pop();
alert(res);
Soubhagya Kumar Barik
  • 1,979
  • 20
  • 26
0

this would cover all possible cases.

window.location.href.split("/").pop().split("?")[0];
0

Though the solution @BalusC provided will work for the question's context. But if the url contains some query params like the one below it will not work.

http://www.example.com/234234234?limit=10&sort=desc

In this scenario you can use the following:

const url = "http://www.example.com/234234234?limit=10&sort=desc"

const id = url.substring(url.lastIndexOf('/') + 1, url.indexOf("?"));

Now id will contain only the id number 234234234.

Hope this helps.

Simon Gomes
  • 383
  • 1
  • 5
  • 18