Permalink
Browse files

abstract backward movement in history, Fixes #4950

`window.history.back` doesn't work in phonegap applications
after a page refresh, though it does work under hashchange/replacestate.
The solution is to use their `navigator.app.backHistory` method
along with a configuration option. The reasoning for the option
is to prevent any corner cases popping up with existing phonegap
applications. Forward history movement for the same usecase is _not_
addressed but remains an even lower priority.
  • Loading branch information...
johnbender committed Sep 12, 2012
1 parent d478003 commit bd94e54e17d24d77bb488392ba750db4df52f6c6
@@ -411,10 +411,27 @@ define( [
} : undefined;
/*
internal utility functions
--------------------------------------*/
/* internal utility functions */
// NOTE Issue #4950 Android phonegap doesn't navigate back properly
// when a full page refresh has taken place. It appears that hashchange
// and replacestate history alterations work fine but we need to support
// both forms of history traversal in our code that uses backward history
// movement
$.mobile.back = function() {
var nav = window.navigator;
// if the setting is on and the navigator object is
// available use the phonegap navigation capability
if( this.phonegapNavigationEnabled &&
nav &&
nav.app &&
nav.app.backHistory ){
nav.app.backHistory();
} else {
window.history.back();
}
};
//direct focus to the page title, or otherwise first focusable element
$.mobile.focusPage = function ( page ) {
@@ -1329,7 +1346,7 @@ define( [
//if there's a data-rel=back attr, go back in history
if ( $link.is( ":jqmData(rel='back')" ) ) {
window.history.back();
$.mobile.back();
return false;
}
@@ -1463,7 +1480,7 @@ define( [
//the current dialog
urlHistory.directHashChange({
currentUrl: to,
isBack: function() { window.history.back(); },
isBack: function() { $.mobile.back(); },
isForward: function() { window.history.forward(); }
});
@@ -95,7 +95,7 @@ define( [ "jquery", "./jquery.mobile.navigation", "depend!./jquery.hashchange[jq
// Note that in some cases we might be replacing an url with the
// same url. We do this anyways because we need to make sure that
// all of our history entries have a state object associated with
// them. This allows us to work around the case where window.history.back()
// them. This allows us to work around the case where $.mobile.back()
// is called to transition from an external page to an embedded page.
// In that particular case, a hashchange event is *NOT* generated by the browser.
// Ensuring each history entry has a state object means that onPopState()
View
@@ -88,7 +88,7 @@ $.widget( "mobile.dialog", $.mobile.widget, {
if ( this._isCloseable ) {
this._isCloseable = false;
if ( $.mobile.hashListeningEnabled ) {
window.history.back();
$.mobile.back();
} else {
dst = $.mobile.urlHistory.getPrev().url;
if ( !$.mobile.path.isPath( dst ) ) {
@@ -315,7 +315,7 @@ define( [
// doesn't solve the possible issue with calling change page
// where the objects don't define data urls which prevents dialog key
// stripping - changePage has incoming refactor
window.history.back();
$.mobile.back();
} else {
self.listbox.popup( "close" );
self.list.appendTo( self.listbox );
View
@@ -753,7 +753,7 @@ define( [ "jquery",
}
if( this.options.history ) {
window.history.back();
$.mobile.back();
} else {
this._close();
}
@@ -249,4 +249,22 @@
equal( $.mobile.path.getLocation( allUriParts ), allUriParts.replace( "jblas:password@", "") );
});
test( "calling mobile back uses phonegap's navigator object when present", function() {
var previous = $.mobile.phonegapNavigationEnabled;
expect( 1 );
$.mobile.phonegapNavigationEnabled = true;
window.navigator = {
app: {
backHistory: function() {
ok( true, "history back called" );
}
}
};
$.mobile.back();
$.mobile.phonegapNavigationEnabled = previous;
});
})(jQuery);

2 comments on commit bd94e54

@toddparker

This comment has been minimized.

Show comment
Hide comment
@toddparker

toddparker Sep 12, 2012

Contributor

Nice fix @johnbender

Contributor

toddparker replied Sep 12, 2012

Nice fix @johnbender

@scottjehl

This comment has been minimized.

Show comment
Hide comment
@scottjehl

scottjehl Sep 13, 2012

Contributor

Hmm, yes. +1 :)

Contributor

scottjehl replied Sep 13, 2012

Hmm, yes. +1 :)

Please sign in to comment.