Skip to content

Commit d9a10c9

Browse files
committed
Keep hash in temporarily changed URL while the page is loading
If you navigate with pjax to `/foo.html#bar`, the URL bar will change to `/foo.html` while the page is loading, but only add `#bar` when it's finished. There's some debate about whether we should be changing the URL at all while loading, because browsers don't seem to do it until the page starts sending data back. However, while we're still changing the URL, it's worth ensuring that at least that URL is complete.
1 parent d6dbbac commit d9a10c9

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

jquery.pjax.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,9 @@ function pjax(options) {
225225
settings.timeout = 0
226226
}
227227

228-
options.requestUrl = stripInternalParams(parseURL(settings.url).href)
228+
var url = parseURL(settings.url)
229+
url.hash = hash
230+
options.requestUrl = stripInternalParams(url.href)
229231
}
230232

231233
options.complete = function(xhr, textStatus) {
@@ -567,8 +569,8 @@ function cloneContents(container) {
567569
// Returns String.
568570
function stripParam(url, name) {
569571
return url
570-
.replace(new RegExp('[?&]' + name + '=[^&]*'), '')
571-
.replace(/[?&]$/, '')
572+
.replace(new RegExp('[?&]' + name + '=[^&#]*'), '')
573+
.replace(/[?&]($|#)/, '\1')
572574
.replace(/[?&]/, '?')
573575
}
574576

test/unit/pjax_fallback.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ asyncTest("scrolls to anchor at top page"+s, function() {
123123

124124
this.loaded = function(frame) {
125125
setTimeout(function() {
126+
equal(frame.location.pathname, "/anchor.html")
127+
equal(frame.location.hash, "#top")
126128
equal(frame.window.scrollY, 8)
127129
start()
128130
}, 100)
@@ -132,6 +134,14 @@ asyncTest("scrolls to anchor at top page"+s, function() {
132134
url: "/anchor.html#top",
133135
container: "#main"
134136
})
137+
138+
if (disabled) {
139+
equal(frame.location.pathname, "/home.html")
140+
equal(frame.location.hash, "")
141+
} else {
142+
equal(frame.location.pathname, "/anchor.html")
143+
equal(frame.location.hash, "#top")
144+
}
135145
})
136146

137147
asyncTest("empty anchor doesn't scroll page"+s, function() {

0 commit comments

Comments
 (0)