@@ -423,7 +423,7 @@ if ('state' in window.history) {
423
423
// You probably shouldn't use pjax on pages with other pushState
424
424
// stuff yet.
425
425
function onPjaxPopstate ( event ) {
426
- var previousState = pjax . state ;
426
+ var previousState = pjax . state
427
427
var state = event . state
428
428
429
429
if ( state && state . container ) {
@@ -432,22 +432,26 @@ function onPjaxPopstate(event) {
432
432
// page.
433
433
if ( initialPop && initialURL == state . url ) return
434
434
435
- // If popping back to the same state, just skip.
436
- // Could be clicking back from hashchange rather than a pushState.
437
- if ( pjax . state && pjax . state . id === state . id ) return
435
+ var direction , containerSelector = state . container
438
436
439
- var container = $ ( state . container )
440
- if ( container . length ) {
441
- var direction , contents = cacheMapping [ state . id ]
437
+ if ( previousState ) {
438
+ // If popping back to the same state, just skip.
439
+ // Could be clicking back from hashchange rather than a pushState.
440
+ if ( previousState . id === state . id ) return
441
+
442
+ // Since state IDs always increase, we can deduce the navigation direction
443
+ direction = previousState . id < state . id ? 'forward' : 'back'
444
+ if ( direction == 'back' ) containerSelector = previousState . container
445
+ }
442
446
443
- if ( pjax . state ) {
444
- // Since state ids always increase, we can deduce the history
445
- // direction from the previous state.
446
- direction = pjax . state . id < state . id ? 'forward' : 'back'
447
+ var container = $ ( containerSelector )
448
+ if ( container . length ) {
449
+ var contents = cacheMapping [ state . id ]
447
450
451
+ if ( previousState ) {
448
452
// Cache current container before replacement and inform the
449
453
// cache which direction the history shifted.
450
- cachePop ( direction , pjax . state . id , cloneContents ( container ) )
454
+ cachePop ( direction , previousState . id , cloneContents ( container ) )
451
455
}
452
456
453
457
var popstateEvent = $ . Event ( 'pjax:popstate' , {
0 commit comments