Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get ID from URL with jQuery

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?

like image 937
wesbos Avatar asked Sep 16 '10 20:09

wesbos


People also ask

How to retrieve id from URL?

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.

How do I find URL parameters?

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.


2 Answers

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.

like image 91
BalusC Avatar answered Oct 02 '22 18:10

BalusC


var url = window.location.pathname; var id = url.substring(url.lastIndexOf('/') + 1); 
like image 38
Rohrbs Avatar answered Oct 02 '22 18:10

Rohrbs