Skip to content

Commit

Permalink
Simplify & improve detection of same-page anchors
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mislav committed Nov 22, 2014
1 parent 93ccd72 commit fa3809d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
19 changes: 12 additions & 7 deletions jquery.pjax.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,8 @@ function handleClick(event, container, options) {
if ( location.protocol !== link.protocol || location.hostname !== link.hostname )
return

// Ignore anchors on the same page
if (link.hash && link.href.replace(link.hash, '') ===
location.href.replace(location.hash, ''))
return

// Ignore empty anchor "foo.html#"
if (link.href === location.href + '#')
// Ignore case when a hash is being tacked on the current URL
if ( link.href.indexOf('#') > -1 && stripHash(link) == stripHash(location) )
return

// Ignore event with default prevented
Expand Down Expand Up @@ -568,6 +563,16 @@ function parseURL(url) {
return a
}

// Internal: Return the `href` component of given URL object with the hash
// portion removed.
//
// location - Location or HTMLAnchorElement
//
// Returns String
function stripHash(location) {
return location.href.replace(/#.*/, '')
}

// Internal: Build options Object for arguments.
//
// For convenience the first parameter can be either the container or
Expand Down
27 changes: 24 additions & 3 deletions test/unit/fn_pjax.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,34 @@ if ($.support.pjax) {
})

asyncTest("ignores same page anchors", function() {
var frame = this.frame
var event, frame = this.frame

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

var event = frame.$.Event('click')
event = frame.$.Event('click')
frame.$("a[href='#main']").trigger(event)
notEqual(event.result, false)
equal(event.isDefaultPrevented(), false)

event = frame.$.Event('click')
frame.$("a[href='#']").trigger(event)
equal(event.isDefaultPrevented(), false)

start()
})

asyncTest("ignores same page anchors from URL that has hash", function() {
var event, frame = this.frame

frame.window.location = "#foo"
frame.$("#main").pjax("a")

event = frame.$.Event('click')
frame.$("a[href='#main']").trigger(event)
equal(event.isDefaultPrevented(), false)

event = frame.$.Event('click')
frame.$("a[href='#']").trigger(event)
equal(event.isDefaultPrevented(), false)

start()
})
Expand Down
1 change: 1 addition & 0 deletions test/views/home.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<li><a href="/aliens.html">aliens</a></li>
<li><a href="https://www.google.com/">Google</a></li>
<li><a href="#main">Main</a></li>
<li><a href="#">Empty</a></li>
</ul>

<form action="env.html" method="GET">
Expand Down

0 comments on commit fa3809d

Please sign in to comment.