To ensure easy access to any part of a path at any time, consider creating a function that accepts an index parameter corresponding to the desired part.
getPathPart = function(index){
if (index === undefined)
index = 0;
var path = window.location.pathname,
parts = path.split('/');
if (parts && parts.length > 1)
parts = (parts || []).splice(1);
return parts.length > index ? parts[index] : null;
}
You can enhance this functionality by adding a flag like getLastIndex. When set to true, it will return the last part of the path.
getPathPart = function(index, getLastIndex){
if (index === undefined)
index = 0;
var path = window.location.pathname,
parts = path.split('/');
if (parts && parts.length > 1)
parts = (parts || []).splice(1);
if(getLastIndex){
return parts[parts.length - 1]
}
return parts.length > index ? parts[index] : null;
}