JavaScript Router API Reference

Router

The Router class defines the router for the entire application. It contains the entry DOM element (in which all dynamic content should be rendered), an instance of Route that should be shown on 404 error, an instance of Route that should be shown between page switches and the base URL in which all internal urls derive from.

new Router(entryDOM, notFoundRoute, loadingRoute = null, baseUrl = '/'): Creates the Router instance.

Route

The Route class defines a single route in the application. It contains the path, the HTML content to display, and any associated JavaScript files to execute when the route is activated.

new Route(path, html, script): Initializes a route with a path, HTML content, and an optional script.

Methods

Router Methods

navigateTo(path): Navigates to a specified path and updates the application state.

addRoute(route): Adds a Route object to the router.

init(): Initializes the router, sets up event listeners, and handles the initial route.

getCurrentPath(): Returns the current active path.

getCurrentRoute(): Returns the current active Route object.

isPathActive(path): Returns a boolean, true or false depending on the paths status.

isRouteActive(route): Returns a boolean, true or false depending on the Route active state.

Route Methods

getHtmlContent(): Returns the HTML content of the route.

getScript(): Returns the full path to the JavaScript file associated with the route. Derived from the specified Base URL