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?
To Get the ID from URL with JavaScript, we can call the JavaScript string's split method on the URL string. Then we get the last part of it with the JavaScript array at method. We call split with '/' to split the url string by the / . Then we call at with -1 to get the element from the strs array.
Method 1: Using the URLSearchParams Object The URLSearchParams is an interface used to provide methods that can be used to work with an URL. The URL string is first separated to get only the parameters portion of the URL. The split() method is used on the given URL with the “?” separator.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With