router.js


A Javascript library for single-page routing done easily

Demo

> Links

> Output

Explanation

To define your routes you need to invoke either the Router.add() or even shorter route() function. The required arguments are pretty simple.

For every route you need to pass a pair of two arguments:

  1. the hash of the route
  2. the callback function that gets invoked after a route match

route(
  '< hash 1 >', function() {
    // do something
  },
  '< hash 2 >', function() {
    // do something
  }
);

Optionally, you can match params using either of the following formats:

  1. :name to match a segment within the hash delimited with slashes
  2. *name to match a segment NOT necessarily delimited with slashes
Params will be available as arguments within the callback function.

route(
  'get/:id', function(id) {
    // do something
  }
);

Setup

These are the routes defined for this demo page.

The Javascript library router.js is written by Paul Engel.