Skip to content

Commit fab2641

Browse files
committed
Scroll to named anchors after pjax navigation
Only elements with IDs were supported before. But since the browser will natively also scroll to matching `<a name="...">` elements, we need to support those as well.
1 parent a958cd6 commit fab2641

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

jquery.pjax.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,8 @@ function pjax(options) {
327327
pjax.state.url = url.href
328328
window.history.replaceState(pjax.state, container.title, url.href)
329329

330-
var target = document.getElementById(url.hash.slice(1))
330+
var name = hash.slice(1)
331+
var target = document.getElementById(name) || document.getElementsByName(name)[0]
331332
if (target) $(window).scrollTop($(target).offset().top)
332333
}
333334

test/views/anchor.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<%= title 'Anchor' %>
22
<div id="top" style="width:500px;height:10000px;"></div>
3-
<div id="bottom" style="width:500px;height:10000px;"></div>
3+
<a name="bottom" style="width:500px;height:10000px;display:block;"></a>
44
<script type="text/javascript">window.parent.iframeLoad(window)</script>

0 commit comments

Comments
 (0)