2

I have one single-page application with three sub-pages.

Url structure is:

/page  (sub-page-0)

/page#sub-page-1

/page#sub-page-2

View Model is:

function ViewModel(activePage){
   var self=this;

   self.activePage=ko.observable(activePage);
   self.changePage=function (newPage){
       self.activePage(newPage);
       //change url
   });
}

When user enter url I want to get # part in javascript and construct ViewModel with this parameter.

When user click link in the page I want to change URL and load that sub page.

Oguz Karadenizli
  • 3,449
  • 6
  • 38
  • 73

2 Answers2

5

You could use one of the solutions from the stack overflow question: How can I get query string values in JavaScript?

However, I strongly recommend you consider a client side routing library. The knockout homepage has an example using sammyjs, but there are many other interesting solutions out there (including knockback, which combines backbonejs and knockoutjs).

Community
  • 1
  • 1
daedalus28
  • 1,637
  • 1
  • 12
  • 24
3

Knockout itself does not support navigation. But you can use third party frameworks. There is a tutorial named "Single page applications" from knockout at http://learn.knockoutjs.com/ which shows how to do that with sammy.js.

delixfe
  • 2,471
  • 1
  • 21
  • 35