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?

0

15 Answers 15

73

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.

Sign up to request clarification or add additional context in comments.

4 Comments

This works great! But what if there is url-stuff on the right side of the url?
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. "example.com/234234234?lang=en" or "example.com/234234234#tab-id" etc..
@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: okay, sure, I fully agree with you. But I just wanted to point out of possible failures/cases for future readers.
58
var url = window.location.pathname;
var id = url.substring(url.lastIndexOf('/') + 1);

Comments

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

Comments

8
var url = "http://www.site.com/234234234"
var stuff = url.split('/');
var id = stuff[stuff.length-1];
//id = 234234234

Comments

5
const url = "http://www.example.com/1234"
const id = url.split('/').pop();

Try this, it is much easier

The output gives 1234

Comments

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.

Comments

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);

Comments

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 "/";

1 Comment

this didn't work for me, but Rohrbs post above worked great.
1

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

jQuery.url.segment(1)

1 Comment

I like this one because most other of the proposed solutions seem to fail when there are anchors, query parameters, ... involved.
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"

Comments

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

Comments

0

Try this

var url = "http://www.exmple.com/234234234"
var res = url.split("/").pop();
alert(res);

Comments

0

this would cover all possible cases.

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

Comments

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.

Comments

0

You can easily use this JQuery code:

<script>
$(function () {
    var getURL = new URL(window.location.href);
    var Id = getURL.pathname.split('/').pop();
});
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.