Implementation of Router and URL Hash in Backbone.js


This article explains the router hashing techniques. In this tutorial we will create a  table of, and generates numbers link. When we click on the link then the related number will be focus.

We know the backbone features it is a lightweight framework that helps to create web projects that are more powerful. And it also provides the structure of JavaScript code in the Model View Controller. Here we use the basic implementation of the URL hashing with the router in Backbone's. Here we need to add these three JavaScript code:

<script src="js/jquery.min.js"></script>

    <script src="js/underscore.js"></script>

    <script src="js/backbone.js"></script>

Solution Explorer View 

Now let's begin to create the application:

Step 1

  • Start Visual Studio 2013. From the Start window select "New Project".
  • Select "Installed" -> "Template" -> "Visual C#" -> "Web" -> "Visual Studio 2012" and select "Empty Web Application".

Select Empty Web Application

  • Click on the OK button.

Step 2

Now add the HTML page.

  • In the Solution Explorer.
  • Right-click on the project then select "Add" -> "HTML page".

Add HTML page

  • Change the name of the page.

Change name of the page

  • Click on the "Add" button.

Add the following code:

<!DOCTYPE html>

<html xmlns="">



    <link href="style.css" rel="stylesheet" />

    <script src="js/jquery.min.js"></script>

    <script src="js/underscore.js"></script>

    <script src="js/backbone.js"></script>

    <script src="js/router.js"></script>



    <a class=" anchor" href="#one">One</a>

    <a class=" anchor" href="#two">Two</a>

    <a class=" anchor" href="#three">Three</a>

    <a class=" anchor" href="#Four">Four</a>

    <a class=" anchor" href="#Five">Five</a>

    <a class=" anchor" href="#Six">Six</a>

    <table class="val">


            <td id="one">1</td>

            <td id="two">2</td>



            <td id="three">3</td>

            <td id="Four">4</td>



            <td id="Five">5</td>

            <td id="Six">6</td>



    <script type='text/javascript'>

        $(document).ready(function () {

            var appRouter = new AppRouter(); 



        function grid(level) {


            $("#" + level).css("background-color""#80c8e5"); 


        // Appending URL values into block

        function gridNo(level, no) {

            grid(level); // Calling grid function

            $("#" + level).html(no); // Appending num value






In the code above we use the #(hash) in the anchor tag that is related to the "router.js" script file.

Step 3

Now in the project we add the "stylesheet".

  • Right-click on the project.
  • Select "New Item" and then "Style Sheet".


  • Click on the "Add" button.

Add the following code to this stylesheet:

.val {













    padding:20px 20px 20px 20px;



    text-decoration :underline;


Step 4

Add the JavaScripts to our project.

Now we need to add various JavaScript files to our application. First we add our own script file in this project.

  • Right-click on the Scripts folder and select "Add".
  • Select "New Item" ->"JavaScript".

javascript file

  • Click on the OK button.

Add the following code:

var AppRouter = Backbone.Router.extend({

    routes: {







    home: function () { 


    blockGrid: function (level) {

        grid(level); // Calling grid function 


     blockNo: function (level, no) {

        gridNo(level, no); // Calling gridNum function



Step 5

Now execute the application, the output will be like this:

Display Table

Now we click on the link:

Working of link 

Click on the browser back and forward buttons and notice the page URL.