Skip to content
This repository was archived by the owner on Nov 10, 2020. It is now read-only.

Commit aec4d63

Browse files
committed
Use History API for hash changes.
1 parent e99f6b5 commit aec4d63

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

_assets/js/swift-site.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,20 +139,22 @@ $(function() {
139139
$(link).html( $(link).html().replace(/^(init|subscript)/, '<span class="identifier">$1</span>') );
140140

141141
// expand the hashed item on page load
142-
var hash = link.href.substring(link.href.lastIndexOf('#'));
142+
var hash = link.href.substring(link.href.lastIndexOf('#')).replace('#comment-', '#');
143143
if (hash == location.hash) link.click();
144144
});
145145

146146
// add toggle actions to all toggle links
147147
$('.toggle-link').click(function() {
148-
var hash = $(this)[0].href.substring($(this)[0].href.lastIndexOf('#') + 1);
148+
var hash = '#' + $(this)[0].href.substring($(this)[0].href.lastIndexOf('#') + 1).replace(/^comment-/, '');
149149

150150
if (!$(this).nextAll('.collapse').hasClass('in')) {
151151
// only set the hash if we're opening this toggle item
152-
location.hash = hash;
152+
if (history.pushState) history.pushState(null, null, hash);
153+
else location.hash = hash;
153154
} else if (location.hash == hash) {
154155
// and clear the hash if we're closing the currently hashed item
155-
location.hash = '';
156+
if (history.pushState) history.pushState(null, null, '');
157+
else location.hash = '';
156158
}
157159
});
158160

0 commit comments

Comments
 (0)