Created
September 15, 2020 23:13
-
-
Save kuntau/193d2050bdeef1d6c7106546bfb76f7b to your computer and use it in GitHub Desktop.
Get last URL segment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PENDING TESTING.. | |
// Get pathname | |
var pathName = window.location.pathname; | |
getLastSegment = function(pathName) { | |
// declare local | |
var lastSegment = -1; //false by default | |
// Check if we even have a pathname | |
if (pathName.length > 1) { | |
// Check if last char is '/' and remove it if so | |
if (pathName.substr(-1) === '/') { | |
pathName = pathName.substr(0, pathName.length - 1); | |
} | |
// Take the last index of '/' and +1 to skip the '/' | |
lastSegment = pathName.substr(pathName.lastIndexOf('/') + 1); | |
} else { | |
// return error if no path | |
lastSegment = -1 | |
} | |
// return result | |
return lastSegment | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment