Skip to content

Commit 549ee90

Browse files
author
scottjehl
committed
initial support for making hash updates and ajax usage contingent upon $.support.hashMakesHistory
1 parent 5d5336d commit 549ee90

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

js/jquery.mobile.navigation.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@
524524

525525
//bind to form submit events, handle with Ajax
526526
$( "form[data-ajax!='false']" ).live('submit', function(event){
527-
if( !$.mobile.ajaxEnabled ||
527+
if( !$.mobile.ajaxEnabled || !$.support.hashMakesHistory ||
528528
//TODO: deprecated - remove at 1.0
529529
!$.mobile.ajaxFormsEnabled ){ return; }
530530

@@ -575,7 +575,7 @@
575575

576576
$activeClickedLink = $this.closest( ".ui-btn" ).addClass( $.mobile.activeBtnClass );
577577

578-
if( isExternal || hasTarget || !$.mobile.ajaxEnabled ||
578+
if( isExternal || hasTarget || !$.mobile.ajaxEnabled || !$.support.hashMakesHistory ||
579579
// TODO: deprecated - remove at 1.0
580580
!$.mobile.ajaxLinksEnabled ){
581581
//remove active link class if external (then it won't be there if you come back)

js/jquery.mobile.support.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,57 @@ function baseTagTest(){
4646
return rebase.indexOf(fauxBase) === 0;
4747
};
4848

49+
//support test for whether a hash change makes a history entry
50+
//not called until the page navigation needs to make sure ajax usage is safe
51+
function testHashMakesHistory(){
52+
var win = window,
53+
count = 0,
54+
addItem = function(){
55+
win.location.hash += "*";
56+
},
57+
prevHash,
58+
histLength,
59+
historyMade;
60+
61+
//set prevHash to current hash
62+
prevHash = win.location.hash;
63+
64+
//disable hash listening
65+
$.mobile.urlHistory.listeningEnabled = false;
66+
67+
//bind to resulting hashchange, set back again after 2 changes
68+
$( win ).bind("hashchange.hashMakesHistory",function(){
69+
count++;
70+
if( count == 2 ){
71+
$.mobile.urlHistory.listeningEnabled = true;
72+
$( win ).unbind("hashchange.hashMakesHistory");
73+
}
74+
});
75+
76+
//change hash, clears forward "history"
77+
addItem();
78+
79+
//get hist length
80+
histLength = win.history.length;
81+
82+
//add another item for comparison
83+
addItem();
84+
85+
//define support bool
86+
historyMade = histLength < win.history.length;
87+
88+
//if it made a history entry, we can go back one to remove the faux entry
89+
if( historyMade ){
90+
history.go( -2 );
91+
}
92+
//otherwise, just put things where they were before because it doesn't matter
93+
else{
94+
win.location.hash = prevHash;
95+
}
96+
97+
return historyMade;
98+
};
99+
49100
$.extend( $.support, {
50101
orientation: "orientation" in window,
51102
touch: "ontouchend" in document,
@@ -63,4 +114,8 @@ fakeBody.remove();
63114
//for ruling out shadows via css
64115
if( !$.support.boxShadow ){ $('html').addClass('ui-mobile-nosupport-boxshadow'); }
65116

117+
$(function(){
118+
$.support.hashMakesHistory = testHashMakesHistory();
119+
});
120+
66121
})( jQuery );

0 commit comments

Comments
 (0)