---
title: "How does Knockout.js handle routing in single-page applications (SPA)?"  
description: "How does Knockout.js handle routing in single-page applications (SPA)?"  
author: "Revati S Misra"  
published: 2024-06-26  
updated: 2024-06-26  
canonical: https://www.mindstick.com/forum/160791/how-does-knockout-js-handle-routing-in-single-page-applications-spa  
category: "knockcout js"  
tags: ["javascript", "knockout.js", "knockout.js routing"]  
reading_time: 3 minutes  

---

# How does Knockout.js handle routing in single-page applications (SPA)?

How does [Knockout.js](https://www.mindstick.com/forum/160797/conditional-html-heading-tag-based-on-variable-in-knockout-js) [handle routing in single-page](https://answers.mindstick.com/qa/112186/how-do-i-handle-routing-in-single-page-applications-spas) [applications](https://www.mindstick.com/articles/12847/how-to-choose-the-right-ethernet-cable-for-industrial-applications) ([SPA](https://www.mindstick.com/articles/44772/5-ways-software-use-can-transform-spa-salon-businesses))?

## Replies

### Reply by Ashutosh Patel

#### Knockout.js Routing

Knockout.js itself does not have built-in [routing](https://www.mindstick.com/articles/11998/attribute-routing-in-asp-dot-net-mvc) capabilities like some other SPA frameworks (e.g., Angular, React with React Router). However, you can add routing to Knockout.js applications by using libraries or using custom solutions. Here’s how you can get closer.

## Third-party libraries

Use libraries like `Sammy.js` or `Durandal.js` (which builds on top of **Knockout.js** and provides routing among other things).

These libraries help in routing in your Knockout.js application.

- **Sammy.js-** Sammy.js is a lightweight JavaScript framework that provides routing capabilities. It can be integrated with Knockout.js to [handle](https://www.mindstick.com/articles/311004/suede-skillet-handle-cover) navigation and routing within the SPA.
- **Durandal.js-** Durandal is a comprehensive framework designed for SPAs. Includes routing, composition, and other out-of-the-box features. It works well with Knockout.js and provides a structured way to manage navigation and navigation.

## Manual Routing

Use manual routing using Knockout.js observables and bindings. This involves creating single-[page](https://www.mindstick.com/articles/13031/why-to-make-a-wikipedia-page) applications where different views (templates) are shown depending on the current route.

- **Observable for Current Route-** Monitor observables on your visual model to monitor current route or view state.
- **Bindings for navigation-** Use Knockout.js bindings (**if**, **visible**, **templates**) to conditionally render your SPA components based on the current path or state.

## Example-

simple example of manual routing with knockout.js

```html
<!DOCTYPE html>
<html data-ng-app="myApp">
<head>
    <meta charset='utf-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.5.1/knockout-latest.min.js"></script>
    <title>Knockout.js Routing Example</title>
</head>
<body>
    <div class="container my-4">
        <nav>
            <ul>
                <li><a href="#" data-bind="click: goToPage.bind($data, 'home')">Home</a></li>
                <li><a href="#" data-bind="click: goToPage.bind($data, 'about')">About</a></li>
                <li><a href="#" data-bind="click: goToPage.bind($data, 'contact')">Contact</a></li>
            </ul>
        </nav>
        <div data-bind="visible: currentRoute() === 'home'">
            <h1>Home Page</h1>
            <p>Welcome to the home page!</p>
        </div>
        <div data-bind="visible: currentRoute() === 'about'">
            <h1>About Page</h1>
            <p>Learn more about us!</p>
        </div>
        <div data-bind="visible: currentRoute() === 'contact'">
            <h1>Contact Page</h1>
            <p>Contact us here.</p>
        </div>
    </div>

    <script>
        function AppViewModel() {
            var self = this;
            self.currentRoute = ko.observable('home');

            self.goToPage = function(route) {
                self.currentRoute(route);
            };
        }

        ko.applyBindings(new AppViewModel());
    </script>
</body>
</html>
```

## In the example above-

- `AppViewModel` manages the current route using `currentRoute` observable.
- Navigation links (`<a>` tags) are bound to `goToPage` function to update the `currentRoute`.
- Different sections (`<div>`s) are shown/hidden based on the current value of `currentRoute`.\

## Output-

![How does Knockout.js handle routing in single-page applications (SPA)?](https://www.mindstick.com/mindstickforums/aaa605de-a35d-444b-b349-b65c2c06123e/images/80c3a464-efb1-475b-a4c2-05a13da20273.jpg)

This method gives you basic routing functionality in the Knockout.js application. For more complex information (such as nested routes, routing parameters, etc.), using a dedicated routing library such as **Sammy.js** or **Durandal.js** may be more appropriate


---

Original Source: https://www.mindstick.com/forum/160791/how-does-knockout-js-handle-routing-in-single-page-applications-spa

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
