Reputation: 3587
I have a URL like this:
http://www.url/name/something/#/venue
I need to grab the 'venue' from each URL. I have:
var currentUrl = $(location).attr('href');
var urlParts = currentUrl.split( "/" );
The split breaks up the URL.
I'm not sure how to grab the last part of urlParts
.
Upvotes: 1
Views: 357
Reputation: 193301
Instead of $(location)
why not just use location
object and location.hash
? Like this:
location.hash.replace('#/', '')
For url like http://www.url/name/something/#/venue
it will give you venue
.
Upvotes: 1
Reputation: 27292
To grab the last element in the array urlParts
, just use:
var last = urlParts[urlParts.length-1];
As an alternate way to do this, you could do:
var last = currentUrl.substr( currentUrl.lastIndexOf("/")+1 );
Upvotes: 0
Reputation: 146269
var currentUrl = $(location).attr('href');
var urlParts = currentUrl.split( "/" );
alert(urlParts[urlParts.length-1]);
The fiddle here.
Upvotes: 0
Reputation: 26228
Just grab the last element of the urlParts
array:
var lastPart = urlParts[urlParts.length - 1];
Upvotes: 1