Skip to content

Instantly share code, notes, and snippets.

@scottzirkel
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottzirkel/ba60f6b79983714f45c1 to your computer and use it in GitHub Desktop.
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.
/**
* @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']
@scottzirkel
Copy link
Author

Useful if you are working with dynamic routes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment