Last active
August 29, 2015 14:02
-
-
Save scottzirkel/ba60f6b79983714f45c1 to your computer and use it in GitHub Desktop.
Parses out the segments of the URI and returns either an array of all or the specified 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
/** | |
* @param {Number} segment | |
*/ | |
function uriSegments(segment){ | |
var uri_segments = window.location.pathname.split('/'); | |
if(segment) | |
return uri_segments[segment]; | |
else | |
return uri_segments; | |
}; | |
// Specify the segment to return one element of the URI | |
// 1 2 3 | |
// http://example.com/controller/function/argument | |
uriSegments(2); // returns 'function'. 0 returns an empty string | |
// Leave empty to return segment array | |
uriSegments(); // returns ['', 'controller', 'function', 'argument'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Useful if you are working with dynamic routes.