Created
July 4, 2013 06:16
-
-
Save seangeo/5925286 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function() { | |
/* The JavaScript history API can be a pain. | |
* | |
* For example, if we navigate out to PayPal to | |
* perform a payment, PayPal is now in the history | |
* and all history.back() calls or back button | |
* presses from then on might cause a back navigation | |
* out of the app and back to paypal, confusing the | |
* user with a PayPal error page. | |
* | |
* This work-around will track the current length of | |
* the history while navigating through the app so if | |
* the last history recorded for the app is not the | |
* most recent history state we can jump back to it | |
* instead of just doing a normal history.back(). | |
* | |
* There is a limitation though, if the user further | |
* navigates after returning from the external service, | |
* the history tracking will be reset. So to avoid this | |
* the page they come back to from should only have two | |
* options, (1) redo the transaction, (2) cancel the | |
* transaction. | |
*/ | |
window.popBackToApp = function() { | |
var lastPosition = parseInt(sessionStorage['last-position']); | |
if (lastPosition) { | |
history.go(lastPosition - history.length - 1); | |
} else { | |
history.back(); | |
} | |
}; | |
window.addEventListener('push', function() { | |
sessionStorage['last-position'] = history.length; | |
}); | |
window.addEventListener('popstate', function() { | |
if (history.state !== null) { | |
sessionStorage['last-position'] = history.length; | |
} | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment