The WinJS.Navigation namespace provides state and history management, but in reality it does not actually navigate. To move from one page to another, you need to define a handler function for one of the events in the WinJS.Navigation namespace - this allows you to answer the call to the WinJS.Navigation.navigate method in a way that makes sense for your application.
As a demo, here is a homePage.html file that has a NavBar containing a command that will become a trigger for navigation.
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>NavProject</title> <link href="//Microsoft.WinJS.1.0/css/ui-dark.css" rel="stylesheet" /> <script src="//Microsoft.WinJS.1.0/js/base.js"></script> <script src="//Microsoft.WinJS.1.0/js/ui.js"></script> <link href="/css/default.css" rel="stylesheet" /> <script src="/js/homePage.js"></script> </head> <body> <div id="contentTarget"> <h1>Select a page from the NavBar</h1> </div> <div id="navbar" data-win-control="WinJS.UI.AppBar" data-win-options="{placement:'top'}"> <button data-win-control="WinJS.UI.AppBarCommand" data-win-options="{id:'NextPage', label:'Next Page', icon:'\u0031', section:'selection'}"> </button> </div> </body> </html>
Along with NavBar, I defined a div element whose id is contentTarget . This is the place in my content where the new file will be uploaded when the user clicks on the NavBar command.
THE CONFIRMATION. All content that you want to replace should go into the contentTarget element. Otherwise, you will get a combination of old and new content.
And here is the JavaScript file that calls it (this is the homePage.js file to which I added the script element for the HTML file above):
(function () { "use strict"; WinJS.Navigation.addEventListener("navigating", function (e) { var elem = document.getElementById("contentTarget"); WinJS.UI.Animation.exitPage(elem.children).then(function () { WinJS.Utilities.empty(elem); WinJS.UI.Pages.render(e.detail.location, elem) .then(function () { return WinJS.UI.Animation.enterPage(elem.children) }); }); }); var app = WinJS.Application; var activation = Windows.ApplicationModel.Activation; app.onactivated = function (args) { args.setPromise(WinJS.UI.processAll()); navbar.addEventListener("click", function (e) { if (e.target.id == "NextPage") { WinJS.Navigation.navigate("/nextPage.html"); } }, true); }; app.start(); })();
Notice how I added a handler function to the WinJS.Navigation.navigating event. This event is triggered by a call to WinJS.Navigation.navigate , and information about the navigation target is contained in the detail.location property of the event object.
In this example, I delete 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 need to define only one handler for the event. This means that if I have elements in nextPage.html that lead to navigation, I just need to call WinJS.Navigation.navigate without creating a new event handler, for example:
<!DOCTYPE html> <html> <head> <title></title> <script> WinJS.UI.Pages.define("/nextPage.html", { ready: function () { back.addEventListener("click", function () { WinJS.Navigation.navigate("/homePage.html"); }); } }); </script> </head> <body> This is next page. <button id="back">Back</button> </body> </html>