Skip to content

Commit 4bd1ccf

Browse files
author
scottjehl
committed
enable popstate after window onload to avoid the first popstate event that occurs in chrome.
1 parent 5df7525 commit 4bd1ccf

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

js/jquery.mobile.navigation.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@
310310
//toggled internally when location.hash is updated to match the url of a successful page load
311311
ignoreNextHashChange: false,
312312

313-
ignoreNextPopState: false
313+
//start with popstate disabled
314+
ignoreNextPopState: true
314315
},
315316

316317
//define first selector to receive focus when a page is shown
@@ -1122,8 +1123,17 @@
11221123
$.mobile.changePage( href, { transition: transition, reverse: reverse, role: role } );
11231124
event.preventDefault();
11241125
});
1126+
1127+
1128+
// Popstate is disabled until after window onload
1129+
// This is to avoid the initial popstate call that occurs in Chrome at load
1130+
$window.bind( "load", function(){
1131+
setTimeout(function(){
1132+
urlHistory.ignoreNextPopState = false;
1133+
}, 0 );
1134+
});
11251135

1126-
//hashchange event handler
1136+
//hashchange and popstate event handler
11271137
$window.bind( "hashchange popstate", function( e, triggered ) {
11281138
//find first page via hash
11291139
var to = path.get(),
@@ -1134,6 +1144,12 @@
11341144
pushStateSupported = $.support.pushState,
11351145
currHref = location.href.split("#")[0];
11361146

1147+
//popstate is disabled until a delay after onload, due to chrome firing it on every page load
1148+
if( e.type == "popstate" && urlHistory.ignoreNextPopState ){
1149+
return;
1150+
}
1151+
1152+
//replace the hashchange if pushstate is supported!
11371153
if( e.type === "hashchange" && pushStateSupported && !isDialog ){
11381154
if( isGeneratedPage ){
11391155
var splitter = '&' + $.mobile.subPageUrlKey;
@@ -1144,18 +1160,18 @@
11441160

11451161
//if it worked, return here.
11461162
if( location.href.split("#")[0] !== currHref ){
1147-
return;
1163+
// return;
11481164
}
11491165
}
11501166

1151-
1152-
11531167
//if listening is disabled (either globally or temporarily), or it's a dialog hash
1154-
// seems this ignored hashchange stuff is unnecessary with pushstate in play
1155-
if( e.type === "hashchange" && !pushStateSupported && ( !$.mobile.hashListeningEnabled || urlHistory.ignoreNextHashChange ) ) {
1168+
if( e.type === "hashchange" && ( !$.mobile.hashListeningEnabled || urlHistory.ignoreNextHashChange ) ) {
11561169
urlHistory.ignoreNextHashChange = false;
11571170
return;
11581171
}
1172+
1173+
1174+
11591175

11601176
//if it's a generated subpage, like a nested list, only use the
11611177

0 commit comments

Comments
 (0)