Browser History and Back Button Support

The browser history and the Back button is one of the fundamental features of all modern browsers. Web users all around the world have come to fully depend on it with such strong mindset that does not distinguish between a Web site, a Web application or even an Ajax application.

While the support for browser history and the Back button is a trivial task for the developers of conventional Web applications to implement, Ajax application developers have to do quite a bit of work to support this feature. Morfik applications, like other Ajax applications, also require special treatment for Browser history and the Back button. With Morfik applications, however, most this work is done at the framework level and you do not need to do much, if any at all.

A Technical Overview

The browser history and the Back button are designed to work with pages. The browser history keeps track of the pages that have been visited by the user. The user can press the back button to navigate to a page that was just visited.

In a Morfik application there is only one physical page. This means if the Back button is pressed the browser would close the application and navigate to a previous site. To get around this problem, Morfik uses a technique that is based on the browser's behavior when navigating within a page. Browsers can navigate to an external page or to a different location within the same page. When navigating to a different location in the same page, browsers do not refresh the page. They do however add an entry to the browser history so they can back track. The way this is indicated is by changing the address bar. The address bar is changed to the original URL plus the Hash character (#) followed by a fragment identifier that determines the location in the page.

Morfik takes advantage of this behavior by regarding its Virtual Pages as virtual locations. The Morfik Application Framework therefore updates the address bar every time a virtual page is activated. If the virtual page is a Published URL then the URL minus the BaseURL is used as the fragment identifier. If not published then the Morfik framework uses a number to represent the virtual page.

Limitations with the framework automatic behavior

In cases where the virtual page is not published the Morfik framework has to somehow figure out the definition of the page so that it can add the entry to the browser history at the right time. The Morfik framework uses the topmost form that is being opened as a reference point for adding the entry to the browser history. In cases where there are more than one topmost forms being opened (eg. invoking more than one OpenForm functions in succession) Morfik will add two entries to the browser history. This result in two clicks being required by the user to go back to the previous page which is not desirable. To address the situation you need to add an entry to the browser history manually.

Adding an entry to the browser history manually

In order to manually perform this task, you need to first notify the framework by either disabling this behavior for specific Forms or the entire application.

To disable the automatic behavior for a specific Form you need to set the Form's Browser History property to False. To disable this behavior for the entire application the XApp.AutomaticStateTracking must be set to false at the start of application.

You then need to call XApp.AddCurrentStateToBrowserHistory(VirtualPageID) to add a new entry to the browser history. VituaPageID is what would be displayed in the browser address bar and it can be any name that is compliant with [RFC 3986].