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?
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?
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.
var url = window.location.pathname;
var id = url.substring(url.lastIndexOf('/') + 1);
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
var url = "http://www.site.com/234234234"
var stuff = url.split('/');
var id = stuff[stuff.length-1];
//id = 234234234
const url = "http://www.example.com/1234"
const id = url.split('/').pop();
Try this, it is much easier
The output gives 1234
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.
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);
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 "/";
Using the jQuery URL Parser plugin, you should be able to do this:
jQuery.url.segment(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"
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
Try this
var url = "http://www.exmple.com/234234234"
var res = url.split("/").pop();
alert(res);
this would cover all possible cases.
window.location.href.split("/").pop().split("?")[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.