An abstraction on site navigation wrapped up in a jQuery plugin. When using website analytics, window.location is not sufficient due to the referer not being passed on the request. The plugin resolves this and allows for both aliased and parametrised URLs. I also provide an API helper method, which enables quick and simple ReSTful URL building capability.
$(document).ready(function() {
$.navigateSetup({
api : '/api/',
endpoints : {
'home' : '/michaelpapworth',
'myAwesomePlugin' : '/{user}/{repo}'
}
});
});The examples below are based on the setup above, let's assume the root of the site is http://github.com.
How to navigate to the preconfigured endpoints?
// Address bar will read http://github.com/michaelpapworth
$.navigate('to', 'home');
// Address bar will read http://github.com/michaelpapworth/jQuery.navigate
$.navigate('to', 'myAwesomePlugin', { user : 'michaelpapworth', repo : 'jQuery.navigate' });Want to navigate to a specific URL?
$.navigate('goTo', 'http://github.com/michaelpapworth/jQuery.navigate'); What about your API?
$.ajax({ type : 'GET', url : $.navigate('api', 'resource', 123) })
.done(function(data) {
console.log('This data came from "http://github.com/api/resource/123"');
});I just want the URL?
// Just use the endpoints to build the URL for me
var homepageUrl = $.navigate('url', 'home'); - Fork this repository and create a new branch if you intend to contribute your work.
- Clone the branch to your computer.
- In the console
cd jQuery.navigate && npm run-script build. - Enable the build as you save your work
npm start.
