Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

vanilla-router

Graidenix417MIT1.2.8TypeScript support: included

JS vanilla Router

router, opa, webpack-router, browserify-router

readme

vanilla-router

NPM

NPM

Description

Simplest One Page Application Router for those who want to use it on Vanilla JS

Note: If you want to use the IE compatible version use v1.1.5

Installation

NPM

npm install vanilla-router --save

Bower

bower install vanilla-router --save

Usage

var router = new Router({
    mode: 'history',
    page404: function (path) {
        console.log('"/' + path + '" Page not found');
    }
});

router.add('', function () {
    console.log('Home page');
});

router.add('hello/(:any)', function (name) {
    console.log('Hello, ' + name);
});

router.add('about', function () {
    console.log('About Page');
});

router.addUriListener();

router.navigateTo('hello/World');

Options

mode string

Default: "history" .

  • hash - is for hashbang routes based on window.location.hash
  • history - is for clean url routes based on HTML5 functionality. It is also provide back-compatibility for old browser.

root string

Default: "/" .

Root represents the relative path for the project root.

page404 function

Default: function that log an error in console.

Callback function for 404 page

routes Array

Default: [ ]

Warning! Use it only if you have all stack with RegExp routes.

Methods

Every public methods of Router return the instance of Router, so they can be chained.

add(path, handler, options)

Add a route and handler for this route

Parameters
  • path - string | RegExp
  • handler - callback if the URL will match the path
  • options - object, you can specify before unload callback

string path can contain:

  • parenthesis (( )) - value between them is sent to callback function
  • wildcards (:any, :num, :word) it doesn't pass the value to callback function
  • named placeholder ({variableName}) it pass the value to the callback function
router.add('hello/world', function(){ });
router.add('hello/:word', function(){ });
router.add('hello/(:word)', function(name){ });
router.add('hello/{name}', function(name){ });
router.add(/^hello\/(\w+)/i, function(name){ });

remove(path)

Remove the route from current list of routes by path

Navigate to the specific URI with optional additional state of page silent is a flag, set as true, to skip check for new URL

check()

Check the current URL for a change

back()

Simulate the browser "Back" button

forward()

Simulate the browser "Forward" button

go(step)

Go to a specific step in history

refresh()

Recall current route handler with same arguments

addUriListener()

Add the event listener for URL change

removeUriListener()

Remove the event listener for URL change

reset()

Reset all setting and state of Router

Licence

Released under the MIT license

Copyright (c) 2016 Grigore Odajiu

changelog

Changelog

v1.2.8 / 2020-03-05

  • Fixed typings

v1.2.7 / 2018-06-18

  • Added Typings inside library

v1.2.6 / 2018-02-14

  • Added replace method

v1.2.5 / 2017-11-15

  • Hot fix

v1.2.4 / 2017-11-15

  • Fixed issue related to missing security hook

v1.2.3 / 2017-04-22

  • Added security hooks

v1.2.2 / 2017-01-21

  • Fixed issue related to sync and async processes

v1.2.1 / 2017-01-13

  • Minor fix

v1.2.0 / 2017-01-13

  • Added Async unload callback
  • Fixed the reset state for unload false
  • Remove IE compatibility

v1.1.5 / 2017-01-13

  • Remove the unload callback
  • Fixed Promise error for IE
  • Fixed always popup confirm box on leave page

v1.1.4 / 2017-01-04

  • Fixed the :num wildcard
  • Added async unload callback

v1.1.2 / 2016-08-22

  • Fixed the silent navigate currentPage object
  • Added refresh method

    v1.1.3 / 2016-08-22

  • Fix query string for refresh

v1.1.2 / 2016-08-22

  • Added recall of router handler functionality

v1.1.1 / 2016-08-02

  • Added silent navigation

v1.1.0 / 2016-07-28

  • Added hook for rooter before page loads
  • Added hook for page before it unloads

v1.0.4 / 2016-04-28

  • Added bower package install

v1.0.3 / 2016-04-28

  • Added LICENSE file

v1.0.2 / 2016-04-21

  • Added detailed API documentation

v1.0.1 / 2016-04-18

  • Added to bower repo

v1.0.0 / 2016-04-18

  • Added dist folder with min and dev version

v0.1.5 / 2016-04-16

  • Added back function
  • Added forward function
  • Added go function

v0.1.4 / 2016-04-16

  • replaced listener interval with browser events

v0.1.3 / 2016-04-16

  • added name-placeholder as variable to pass to callback

v0.1.2 / 2016-04-16

  • Fixed multiple wildcards
  • Added name-placeholder as {placeholder}

v0.1.1 / 2016-04-08

  • Added window state

v0.0.4 / 2016-04-02

  • Bug fixing
  • Added unit tests

v0.0.3 / 2016-03-30

  • Added route wildcards :any, :num, :word
  • Parse query string
  • Set Interval as private property
  • Fixed remove route

v0.0.2 / 2016-03-30

  • Fix URI match issue

v0.0.1 / 2016-03-30

  • Added core functionality