Search code examples
windows-8navigationwinjsappbar

Win8 JS App: How can one prevent backward navigation? Can't set WinJS.Navigation.canGoBack


Fairly new to developing for Windows 8, I'm working on an app that has a rather flat model. I have looked and looked, but can't seem to find a clear answer on how to set a WinJS page to prevent backward navigation. I have tried digging into the API, but it doesn't say anything on the matter.

The code I'm attempting to use is

WinJS.Navigation.canGoBack = false;

No luck, it keeps complaining about the property being read only, however, there are no setter methods to change it.

Thanks ahead of time, ~Sean


Solution

  • canGoBack does only have a getter (defined in base.js), and it reflects the absence or presence of the backstack; namely nav.history.backstack.

    The appearance of the button itself is controlled by the disabled attribute on the associated button DOM object, which in turn is part of a CSS selector controlling visibility. So if you do tinker with the display of the Back button yourself be aware that the navigation plumbing is doing the same.

    Setting the backstack explicitly is possible; there's a sample the Navigation and Navigation History Sample that includes restoring a history as well as preventing navigation using beforenavigate, with the following code:

        // in ready
        WinJS.Navigation.addEventListener("beforenavigate", this.beforenavigate);
    
    
        //
        beforenavigate: function (eventObject) {
            // This function gives you a chance to veto navigation. This demonstrates that capability
            if (this.shouldPreventNavigation) {
                WinJS.log && WinJS.log("Navigation to " + eventObject.detail.location + " was prevented", "sample", "status");
                eventObject.preventDefault();
            }
        },