Skip to content

Commit d22c8c3

Browse files
committed
Merge pull request #361 from aroben/fix-back-after-fragment-reload
Fix going back to a pjaxed state after reloading a fragment navigation
2 parents 7f5764c + 6becaf0 commit d22c8c3

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

jquery.pjax.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ function onPjaxPopstate(event) {
404404

405405
// If popping back to the same state, just skip.
406406
// Could be clicking back from hashchange rather than a pushState.
407-
if (pjax.state.id === state.id) return
407+
if (pjax.state && pjax.state.id === state.id) return
408408

409409
var container = $(state.container)
410410
if (container.length) {

test/unit/pjax.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,4 +1011,50 @@ if ($.support.pjax) {
10111011
ok(frame.$.pjax.state.id)
10121012
oldId = frame.$.pjax.state.id
10131013
})
1014+
1015+
asyncTest("handles going back to pjaxed state after reloading a fragment navigation", function() {
1016+
var iframe = this.iframe
1017+
var frame = this.frame
1018+
var supportsHistoryState = 'state' in window.history
1019+
1020+
// Get some pjax state in the history.
1021+
frame.$.pjax({
1022+
url: "hello.html",
1023+
container: "#main",
1024+
})
1025+
frame.$("#main").on("pjax:complete", function() {
1026+
var state = frame.history.state
1027+
ok(frame.$.pjax.state)
1028+
if (supportsHistoryState)
1029+
ok(frame.history.state)
1030+
1031+
// Navigate to a fragment, which will result in a new history entry with
1032+
// no state object. $.pjax.state remains unchanged however.
1033+
iframe.src = frame.location.href + '#foo'
1034+
ok(frame.$.pjax.state)
1035+
if (supportsHistoryState)
1036+
ok(!frame.history.state)
1037+
1038+
// Reload the frame. This will clear out $.pjax.state.
1039+
frame.location.reload()
1040+
$(iframe).one("load", function() {
1041+
ok(!frame.$.pjax.state)
1042+
if (supportsHistoryState)
1043+
ok(!frame.history.state)
1044+
1045+
// Go back to #main. We'll get a popstate event with a pjax state
1046+
// object attached from the initial pjax navigation, even though
1047+
// $.pjax.state is null.
1048+
window.iframeLoad = function() {
1049+
ok(frame.$.pjax.state)
1050+
if (supportsHistoryState) {
1051+
ok(frame.history.state)
1052+
equal(frame.$.pjax.state.id, state.id)
1053+
}
1054+
start()
1055+
}
1056+
frame.history.back()
1057+
})
1058+
})
1059+
})
10141060
}

0 commit comments

Comments
 (0)