Skip to content

Commit fa3809d

Browse files
committed
Simplify & improve detection of same-page anchors
If you were on URL that has `#foo` and clicked on link that has simply `#` as href, pjax wouldn't recognize it as same-page anchor and would allow xhr request to be initiated.
1 parent 93ccd72 commit fa3809d

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

jquery.pjax.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,8 @@ function handleClick(event, container, options) {
7575
if ( location.protocol !== link.protocol || location.hostname !== link.hostname )
7676
return
7777

78-
// Ignore anchors on the same page
79-
if (link.hash && link.href.replace(link.hash, '') ===
80-
location.href.replace(location.hash, ''))
81-
return
82-
83-
// Ignore empty anchor "foo.html#"
84-
if (link.href === location.href + '#')
78+
// Ignore case when a hash is being tacked on the current URL
79+
if ( link.href.indexOf('#') > -1 && stripHash(link) == stripHash(location) )
8580
return
8681

8782
// Ignore event with default prevented
@@ -568,6 +563,16 @@ function parseURL(url) {
568563
return a
569564
}
570565

566+
// Internal: Return the `href` component of given URL object with the hash
567+
// portion removed.
568+
//
569+
// location - Location or HTMLAnchorElement
570+
//
571+
// Returns String
572+
function stripHash(location) {
573+
return location.href.replace(/#.*/, '')
574+
}
575+
571576
// Internal: Build options Object for arguments.
572577
//
573578
// For convenience the first parameter can be either the container or

test/unit/fn_pjax.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,34 @@ if ($.support.pjax) {
180180
})
181181

182182
asyncTest("ignores same page anchors", function() {
183-
var frame = this.frame
183+
var event, frame = this.frame
184184

185185
frame.$("#main").pjax("a")
186186

187-
var event = frame.$.Event('click')
187+
event = frame.$.Event('click')
188188
frame.$("a[href='#main']").trigger(event)
189-
notEqual(event.result, false)
189+
equal(event.isDefaultPrevented(), false)
190+
191+
event = frame.$.Event('click')
192+
frame.$("a[href='#']").trigger(event)
193+
equal(event.isDefaultPrevented(), false)
194+
195+
start()
196+
})
197+
198+
asyncTest("ignores same page anchors from URL that has hash", function() {
199+
var event, frame = this.frame
200+
201+
frame.window.location = "#foo"
202+
frame.$("#main").pjax("a")
203+
204+
event = frame.$.Event('click')
205+
frame.$("a[href='#main']").trigger(event)
206+
equal(event.isDefaultPrevented(), false)
207+
208+
event = frame.$.Event('click')
209+
frame.$("a[href='#']").trigger(event)
210+
equal(event.isDefaultPrevented(), false)
190211

191212
start()
192213
})

test/views/home.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<li><a href="/aliens.html">aliens</a></li>
77
<li><a href="https://www.google.com/">Google</a></li>
88
<li><a href="#main">Main</a></li>
9+
<li><a href="#">Empty</a></li>
910
</ul>
1011

1112
<form action="env.html" method="GET">

0 commit comments

Comments
 (0)