Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to extract last value in the URL using JQuery

Tags:

jquery

how to extract the last value that is 1 from the following Url using jQuery...

Url : /FormBuilder/index.php/reports/export/1

like image 293
useranon Avatar asked Sep 18 '09 06:09

useranon


People also ask

How do I pull the last part of a URL?

You can also use the lastIndexOf() function to locate the last occurrence of the / character in your URL, then the substring() function to return the substring starting from that location: console. log(this. href.

How to get last word in URL in JQuery?

You can also make use of JQuery lastindexof and find the last word.

How can I get URL in JQuery?

The current URL in jQuery can be obtained by using the 'href' property of the Location object which contains information about the current URL. The 'href' property returns a string with the full URL of the current page.

How do I find the URL ID?

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.


2 Answers

As you can see from all of the answers JQuery isn't needed to do this.

You could split it:

var url = 'www.google.com/dir1/dir2/2';
var id = parseInt(url.split('/')[url.split('/').length - 1]);
like image 197
Brian Avatar answered Oct 14 '22 19:10

Brian


You can use substring and lastIndexOf:

var value = url.substring(url.lastIndexOf('/') + 1);

If the second parameter of substring is omitted, it extracts the characters to the end of the string.

like image 28
Christian C. Salvadó Avatar answered Oct 14 '22 19:10

Christian C. Salvadó