Router is a simple-to-use JavaScript router and window.history manager. Routes can be defined as simple strings or complex regular expressions.
There are a lot of great open-source routers but they are often large, complex, or tied to a framework. Router is designed to be small, readable, and adaptable to your project.
<ul>
<li><a href="/" data-route="href">Home</a></li>
<li><a href="#" data-route="/shop">Shop</a></li>
<li><a href="/product/123" data-route="href">Product 123</a></li>
</ul>
<script src="Router.js"></script>
<script>
function onRouteChange(data) {
switch (data.id) {
case 'home' :
console.log('Catch all route.');
break;
case 'shop' :
console.log('Shop route.');
break;
case 'product-page' :
console.log('Product route id: ' + data.matches[0]);
break;
}
}
var router = new Router();
router.addRoute({
id: 'shop',
route: '/shop',
callback: onRouteChange
}).addRoute({
id: 'product-page',
route: '/product/([0-9]+)',
callback: onRouteChange
}).addRoute({
id: 'home',
route: '.*', // Catch all RegExp
callback: onRouteChange
});
// Triggers the initial landing route
router.requestRoute();
</script>Creates a new instance of Router.
- [options]
Object- Defines optional settings:- [options.useHash]
Boolean- Specifies ifRoutershould use#hash / fragment identifiers instead of theHistoryAPI; defaults tofalse. If the browser does not support theHistoryAPI,Routerwill automatically fallback to using fragment indentifiers. - [options.disableListeners]
Boolean- Specifies ifRoutershouldn’t add events to listen for route data attributes; defaults tofalse.
- [options.useHash]
Adds a new route. Only the first matched route will be triggered; routes are compared in the order in which they are added to Router. This method returns the instance of Router to allow for method chaining.
- settings
Object- An object that defines the routes settings and callback function.- [settings.id]
String- An optional identifier that is passed to thesettings.callbackfunction. If undefined, the string provided forsettings.routewill be used as the identifier. - settings.route
String- The route pattern to match. - settings.callback
Function- A function that should be called when the route has been triggered. The callback function is passed an object with two properties:- object.id
String- The route identifier. - object.matches
Array- If the route included RegExp capture groups, the groups will be provided as an array. If the route does not contain any capture groups, an empty array will be provided.
- object.id
- [settings.id]
Requests a pushState and route change.
- [route]
String- If no route is provided,Routerrequests the current route. Requesting the current route may be helpful if you wish to trigger the initial landing route.
Router can detect HTML elements with the data attribute data-route and automatically send the value to requestRoute when the element is clicked.
- route
String- The route to send torequestRoute.
Example:
<a href="#" data-route="/shop">Link</a>Defining data-route="href" will pass the value of the href attribute to the router when the element is clicked:
<a href="/shop" data-route="href">Link</a>Router can be used freely for any open source or commercial works and is released under a MIT license.