how to extract the last value that is 1 from the following Url using jQuery...
Url : /FormBuilder/index.php/reports/export/1
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.
You can also make use of JQuery lastindexof and find the last word.
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.
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.
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]);
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.
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