hash-router

A frontend router for the hash change event

npm install hash-router
3 downloads in the last day
11 downloads in the last week
53 downloads in the last month

hash-router

A frontend router for the hash change event

Example

var HashRouter = require("hash-router")

var router = HashRouter()
router.addRoute("#/", renderHome)
router.addRoute("#/login", showLoginDialog)

router.on("hash", function (hash, event) {
  console.log("hash changed!", hash)
})

window.addEventListener("hashchange", router)
router() // start the router

Docs

type RouteHandler := (hash: String, opts: {
    params: Object,
    splats: Array<String>,
    newUrl: String,
    oldUrl: String
}) => void

hash-router := (opts?: {
    setRoute?: (String) => void,
    getRoute?: () => String
}) => EventEmitter & {
    (HashChangeEvent?) => void,
    go: (String) => void,
    get: () => String,
    addRoute: (pattern: String | RegExp, handler: RouteHandler) => void
}

Installation

npm install hash-router

Contributors

  • Raynos

MIT Licenced

npm loves you