Ello!
I have an app bar icon and on the click event – I added a function which has the following code:
function homePage() {
WinJS.Navigation.navigate("/home/homePage.html");
}
Now I have two files – homePage.html which is inside /home/ and the js file for the same.
There’s a simple button on html of id NextPage.
While in the homePage.js file, I have:
function () {
"use strict";
WinJS.UI.Pages.define("/home/homePage.html", {
ready: function (element, options) {
var button = document.getElementById("NextPage");
button.addEventListener("click", GoToNextPage);
}
});
function GoToNextPage() {
WinJS.Navigation.navigate("/default.html");
}
})();
But when I click the app bar icon – nothing happens 🙁
So what I plan to accomplish is that when someone clicks an appbar icon on default.html – the user switches to homePage.html (and then when I click the homePage button – it goes back) – but not even the initial page transfer is taking place.
This is embarrassing to ask but I can’t just fold my hands and wait for something magical to happen. I have been working on this for an hour – read videos and samples but it’s not working at all.
Would appreciate help – I can’t figure out what’s going wrong. Thanks!
The
WinJS.Navigationnamespace provides state and history management, but it doesn’t actually do the navigation itself. To move from one page to another, you need to define a handler function for one of the events in theWinJS.Navigationnamespace – this lets you respond to call to theWinJS.Navigation.navigatemethod in a way which makes sense for your app.As a demonstration, here is a
homePage.htmlfile which has a NavBar containing a command that will be the trigger for the navigation.Along with the NavBar, I have defined the
divelement whoseidiscontentTarget. This is the place in my content where the new file will be loaded when the user clicks the NavBar command.CLARIFICATION: All of the content that you want replaced needs to go into the
contentTargetelement. Otherwise you’ll get a mix of old and new content displayed.And here is the JavaScript file which wires it up (this is the
homePage.jsfile which I added a script element for in the HTML file above):Notice how I have added a handler function for the
WinJS.Navigation.navigatingevent. This event is triggered by a call toWinJS.Navigation.navigateand details of the navigation target are contained in thedetail.locationproperty of the event object.In this example, I clear out any content in my target element and replace it with the contents of the target file and animate the transition from one to the other.
You only have to define one handler for the event. This means that if I have elements in
nextPage.htmlthat will lead to navigation, I just need to callWinJS.Navigation.navigatewithout needing to create a new event handler, like this: