I've got a url like this:
http://www.site.com/234234234
I need to grab the Id after /
, so in this case 234234234
How can i do this easily?
This question is related to
javascript
jquery
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 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
Try this
var url = "http://www.exmple.com/234234234"
var res = url.split("/").pop();
alert(res);
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);
var url = window.location.pathname;
var id = url.substring(url.lastIndexOf('/') + 1);
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.
var url = "http://www.site.com/234234234"
var stuff = url.split('/');
var id = stuff[stuff.length-1];
//id = 234234234
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"
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)
Source: Stackoverflow.com