Skip to content

Instantly share code, notes, and snippets.

@seangeo
Created July 4, 2013 06:16
Show Gist options
  • Select an option

  • Save seangeo/5925286 to your computer and use it in GitHub Desktop.

Select an option

Save seangeo/5925286 to your computer and use it in GitHub Desktop.

Revisions

  1. seangeo renamed this gist Jul 4, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. seangeo created this gist Jul 4, 2013.
    44 changes: 44 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    $(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;
    }
    });
    });