Skip to content
This repository was archived by the owner on Oct 8, 2021. It is now read-only.

Commit c3b89eb

Browse files
committed
bstract 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.
1 parent 6df2479 commit c3b89eb

File tree

5 files changed

+47
-13
lines changed

5 files changed

+47
-13
lines changed

js/jquery.mobile.dialog.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ $.widget( "mobile.dialog", $.mobile.widget, {
2525
});
2626

2727
$el.addClass( "ui-dialog ui-overlay-" + this.options.overlayTheme );
28-
28+
2929
// Class the markup for dialog styling
3030
// Set aria role
3131
$el
@@ -88,9 +88,8 @@ $.widget( "mobile.dialog", $.mobile.widget, {
8888
if ( !this._isClosed ) {
8989
this._isClosed = true;
9090
if ( $.mobile.hashListeningEnabled ) {
91-
window.history.back();
92-
}
93-
else {
91+
$.mobile.back();
92+
} else {
9493
dst = $.mobile.urlHistory.getPrev().url;
9594
if ( !$.mobile.path.isPath( dst ) ) {
9695
dst = $.mobile.path.makeUrlAbsolute( "#" + dst );

js/jquery.mobile.forms.select.custom.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ define( [
321321
// doesn't solve the possible issue with calling change page
322322
// where the objects don't define data urls which prevents dialog key
323323
// stripping - changePage has incoming refactor
324-
window.history.back();
324+
$.mobile.back();
325325
} else {
326326
self.screen.addClass( "ui-screen-hidden" );
327327
self.listbox.addClass( "ui-selectmenu-hidden" ).removeAttr( "style" ).removeClass( "in" );

js/jquery.mobile.navigation.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -410,10 +410,27 @@ define( [
410410

411411
} : undefined;
412412

413-
/*
414-
internal utility functions
415-
--------------------------------------*/
416-
413+
/* internal utility functions */
414+
415+
// NOTE Issue #4950 Android phonegap doesn't navigate back properly
416+
// when a full page refresh has taken place. It appears that hashchange
417+
// and replacestate history alterations work fine but we need to support
418+
// both forms of history traversal in our code that uses backward history
419+
// movement
420+
$.mobile.back = function() {
421+
var nav = window.navigator;
422+
423+
// if the setting is on and the navigator object is
424+
// available use the phonegap navigation capability
425+
if( this.phonegapNavigationEnabled &&
426+
nav &&
427+
nav.app &&
428+
nav.app.backHistory ){
429+
nav.app.backHistory();
430+
} else {
431+
window.history.back();
432+
}
433+
};
417434

418435
//direct focus to the page title, or otherwise first focusable element
419436
$.mobile.focusPage = function ( page ) {
@@ -1319,8 +1336,8 @@ define( [
13191336
};
13201337

13211338
//if there's a data-rel=back attr, go back in history
1322-
if( $link.is( ":jqmData(rel='back')" ) ) {
1323-
window.history.back();
1339+
if ( $link.is( ":jqmData(rel='back')" ) ) {
1340+
$.mobile.back();
13241341
return false;
13251342
}
13261343

@@ -1445,7 +1462,7 @@ define( [
14451462
//the current dialog
14461463
urlHistory.directHashChange({
14471464
currentUrl: to,
1448-
isBack: function() { window.history.back(); },
1465+
isBack: function() { $.mobile.back(); },
14491466
isForward: function() { window.history.forward(); }
14501467
});
14511468

js/jquery.mobile.navigation.pushstate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ define( [ "jquery", "./jquery.mobile.navigation", "../external/requirejs/depend!
9595
// Note that in some cases we might be replacing an url with the
9696
// same url. We do this anyways because we need to make sure that
9797
// all of our history entries have a state object associated with
98-
// them. This allows us to work around the case where window.history.back()
98+
// them. This allows us to work around the case where $.mobile.back()
9999
// is called to transition from an external page to an embedded page.
100100
// In that particular case, a hashchange event is *NOT* generated by the browser.
101101
// Ensuring each history entry has a state object means that onPopState()

tests/unit/navigation/navigation_helpers.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,4 +249,22 @@
249249

250250
equal( $.mobile.path.getLocation( allUriParts ), allUriParts.replace( "jblas:password@", "") );
251251
});
252+
253+
test( "calling mobile back uses phonegap's navigator object when present", function() {
254+
var previous = $.mobile.phonegapNavigationEnabled;
255+
256+
expect( 1 );
257+
258+
$.mobile.phonegapNavigationEnabled = true;
259+
window.navigator = {
260+
app: {
261+
backHistory: function() {
262+
ok( true, "history back called" );
263+
}
264+
}
265+
};
266+
267+
$.mobile.back();
268+
$.mobile.phonegapNavigationEnabled = previous;
269+
});
252270
})(jQuery);

0 commit comments

Comments
 (0)