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.
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.
Share Improve this question asked Aug 9, 2012 at 8:34 Oguz KaradenizliOguz Karadenizli 3,5057 gold badges43 silver badges75 bronze badges2 Answers
Reset to default 5You could use one of the solutions from the stack overflow question: How can I get query string values in JavaScript?
However, I strongly remend 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 bines backbonejs and knockoutjs).
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./ which shows how to do that with sammy.js.