Skip to content

Commit 885ffb8

Browse files
authored
[WEB-4393] Fix anchor scrolling bug on Chrome browser (DataDog#21120)
* Add overflow style to body to fix scroll https://stackoverflow.com/a/40372844 * tweak script * Update table-of-contents.js * Update table-of-contents.js * Update table-of-contents.js
1 parent 586412c commit 885ffb8

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

assets/scripts/components/table-of-contents.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,6 @@ const tocCloseIcon = document.querySelector('.js-mobile-toc-toggle .icon-small-x
1010
const tocBookIcon = document.querySelector('.js-mobile-toc-toggle .icon-small-bookmark');
1111
const tocEditBtn = document.querySelector('.js-toc-edit-btn');
1212

13-
// Fixes Chrome issue where pages with hash params are not scrolling to anchor
14-
const chromeHashScroll = () => {
15-
const isChrome = /Chrome/.test(navigator.userAgent);
16-
if (window.location.hash && isChrome) {
17-
setTimeout(function () {
18-
const hash = window.location.hash;
19-
window.location.hash = '';
20-
window.location.hash = hash;
21-
}, 300);
22-
}
23-
};
24-
2513
function isTOCDisabled() {
2614
const toc = document.querySelector('#TableOfContents');
2715
if (!toc) {
@@ -231,9 +219,6 @@ function handleAPIPage() {
231219
}
232220
}
233221

234-
DOMReady(chromeHashScroll);
235-
DOMReady(handleAPIPage);
236-
237222
if (tocMobileToggle) {
238223
tocMobileToggle.addEventListener('click', toggleMobileTOC);
239224
}
@@ -250,3 +235,17 @@ window.addEventListener('resize', () => {
250235
window.addEventListener('scroll', () => {
251236
onScroll();
252237
});
238+
239+
DOMReady(handleAPIPage);
240+
241+
// Fixes Chrome issue where pages with hash params are not scrolling to anchor
242+
window.addEventListener('load', () => {
243+
const isChrome = /Chrome/.test(navigator.userAgent);
244+
if (window.location.hash && isChrome) {
245+
setTimeout(function () {
246+
const hash = window.location.hash;
247+
window.location.hash = '';
248+
window.location.hash = hash;
249+
}, 300);
250+
}
251+
});

0 commit comments

Comments
 (0)