I have a url in this form http://www.example.com/data/45. How do I get the last element (45 in this example) using jQuery or JavaScript?
Thanks.
I have a url in this form http://www.example.com/data/45. How do I get the last element (45 in this example) using jQuery or JavaScript?
Thanks.
A very quick plain JavaScript method, assuming it's the URL of the current page (in window.location.href)
var urlNum = window.location.href.split('/').pop();
.split('/') obviously chops the URL up into an array, based on divisions by /. Since you only need the last part, it is probably the easiest method.
.pop() removes the last element from the array returned by .split() and returns it.