bvstone

Using Back and Forward Buttons on a Single Page Application (SPA) Web Site

Posted:

Using Back and Forward Buttons on a Single Page Application (SPA) Web Site

Recently BVSTools went live with their mobile/responsive web site.  We decided at this time to set it up as a Single Page Application (SPA) style web site.

Because of this design, the back and forward buttons don't work traditionally like they would.  This is because each time a "link" is clicked instead of changing the location of the web browser to another web page, we simply replace the content on the main part of the page with the new information.

One day we got a call from a customer who said they loved the new site, but didn't like that the back button didn't work within the site.  I explained that yes, that's an issue with SPA web sites and that we were looking into a solution.

What we came up with was using the new HTML5 pushstate and popstate APIs that are available to manipulate history.  

Because we already had a JavaScript function that would "load" the contents of a link into the page, it was actually quite simple to do.  Our original function was as follows:

function loadBodyContent(path, callback) {

	$.get( path, function( data ) {
		$("#bodyContent").html( data );
		$("#bodyContent").append("<div id='footerContent'></div>");
		$("#footerContent").load("/ssi/footer.html");
		
		if (callback) callback();
	});
	
}

When we look at the loadBodyContent() function we see that we can pass in a path to load (and an optional callback function, which we're not worried about here).

The content from the path is loaded into the bodyContent div of the web  page, and then we tack on the footer portion of the page as well.

Now that we wish to manipulate the history to allow the Back and Forward buttons to work, we updated our function to this:

function loadBodyContent(path, addToHistory, callback) {
	
	$.get( path, function( data ) {
		$("#bodyContent").html( data );
		$("#bodyContent").append("<div id='footerContent'></div>");
		$("#footerContent").load("/ssi/footer.html");
		
		if (addToHistory != false) {
			addHistory(path);
		}
		
		if (callback) callback();
	});
	
}

We see that we have a new (and optional) parameter named addToHistory.  If this value is not false, we call the addHistory() function which looks like this:

function addHistory(path) {
	history.pushState(path, null, null);
}

What this is doing is pushing some information to the history stack.  Most sites will use the 3rd parameter (which is a URI parameter) but we found this also changes the URL in the web page and attempts to load... in our case we don't want that.  So, we used only the first parameter which can be retrieved when the back button is pressed.

Which leads us to the next step... how do we capture the Back or Forward buttons on a browser?  With a window event listener.  We added the following code to our main JavaScript file:

	window.addEventListener('popstate', function(e) {
		var location = e.state;
	
		if (location != null) {
			loadBodyContent(location,false);
			$('.addedMenuNavBarItem').remove();

		} else {
			window.history.back();
		}
	});

Now, when the Back or Forward buttons are pressed, we're able to get the last history item's value (the location).  We check if it's not null, and if it's not, we use it to call the loadBodyContent() function again, this time passing the boolean value of false so this location is not added to the history.

This works pretty slick on the browsers I've tested.  If you want to try things out feel free to visit our responsive site at http://m.bvstools.com.


Last edited 10/06/2015 at 18:59:02



Latest Posts:

Using GETURI to Make OAuth 2.0 Requests - Custom Headers for Access Tokens Using GETURI to Make OAuth 2.0 Requests - Custom Headers for Access Tokens
Posted by 2 days ago
BVSTools >> BVSTools Software Discussion >> Get URI (GETURI) Specific Discussion
GreenTools for Microsoft Apps (G4MS) v5.00 Released with Updated OneDrive Support and 3rd Party Functionality GreenTools for Microsoft Apps (G4MS) v5.00 Released with Updated OneDrive Support and 3rd Party Functionality
Posted by October 20, 2019
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
BVSTools is Now Running V7R4M0 BVSTools is Now Running V7R4M0
Posted by September 28, 2019
BVSTools >> BVSTools Announcements
GreenTools for G Suite (G4G) Now Includes Send Mail Functionality (G4GSMAIL Addon) GreenTools for G Suite (G4G) Now Includes Send Mail Functionality (G4GSMAIL Addon)
Posted by August 27, 2019
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
GreenTools For G Suite (G4G) v12.00 Released With Base OAuth 2.0 Functionality GreenTools For G Suite (G4G) v12.00 Released With Base OAuth 2.0 Functionality
Posted by July 28, 2019
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
BVSTools Small Price Increase in 2020 BVSTools Small Price Increase in 2020
Posted by July 26, 2019
BVSTools >> BVSTools Announcements
GreenTools for Vertex Cloud (VTXCLOUD) Now Available GreenTools for Vertex Cloud (VTXCLOUD) Now Available
Posted by July 22, 2019
BVSTools >> BVSTools Announcements >> GreenTools for Vertex Cloud (VTXCLOUD) Specific Announcements
GreenTools for Google Apps (G4G) - Drive Addon Successfully Verified by Google GreenTools for Google Apps (G4G) - Drive Addon Successfully Verified by Google
Posted by July 22, 2019
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
Why I Cancelled my DynDNS Service and How I Replaced It with an IBM i Application Why I Cancelled my DynDNS Service and How I Replaced It with an IBM i Application
Posted by July 17, 2019
IBM Power Systems >> (QGPL) IBM i
Green Tools for G Suite (G4G) Product Updates (Licensing, Functionality, Base Product) Green Tools for G Suite (G4G) Product Updates (Licensing, Functionality, Base Product)
Posted by July 13, 2019
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
Reading JSON Data from Standard Input With YAJL and RPG Reading JSON Data from Standard Input With YAJL and RPG
Posted by July 12, 2019
Programming >> Proof of Concept (POC)
MAILTOOL Updated to Allow Use of IBM Global Security Kit (GSKIT) for SSL/TLS Communications MAILTOOL Updated to Allow Use of IBM Global Security Kit (GSKIT) for SSL/TLS Communications
Posted by June 19, 2019
BVSTools >> BVSTools Announcements >> eMail Tool (MAILTOOL) Specific Announcements
GETURI v10.00 Released Supporting IBM Global Security Kit (GSKIT) and Server Name Indication (SNI) GETURI v10.00 Released Supporting IBM Global Security Kit (GSKIT) and Server Name Indication (SNI)
Posted by June 11, 2019
BVSTools >> BVSTools Announcements >> Get URI (GETURI) Specific Announcements
BVSTools Now Offers Vertex Cloud Interface BVSTools Now Offers Vertex Cloud Interface
Posted by April 15, 2019
BVSTools >> BVSTools Announcements
Token Has an Invalid Signature Error for Office 365 Email Token Has an Invalid Signature Error for Office 365 Email
Posted by March 22, 2019
BVSTools >> BVSTools Software Discussion >> GreenTools for Microsoft Apps (G4MS) Specific Discussion

Reply




Copyright 1983-2019 BVSTools
GreenBoard(v3) Powered by the eRPG SDK, MAILTOOL Plus!, GreenTools for Google Apps, jQuery, jQuery UI, BlockUI, CKEditor and running on the IBM i (AKA AS/400, iSeries, System i).