Skip to content
Snippets Groups Projects
Commit 18571047 authored by Daniel Kurowski's avatar Daniel Kurowski
Browse files

Do not throw error onload scroll when target does not exist

parent 37f5cdf1
No related branches found
No related tags found
1 merge request!10Do not throw error onload scroll when target does not exist
Pipeline #32779 passed
......@@ -181,15 +181,20 @@ function initializeOnLoadScroll() {
var hashTarget = null;
var start = performance.now();
document.addEventListener('DOMContentLoaded', function () {
hashTarget = HashTarget.fromString(hash, document);
try {
hashTarget = HashTarget.fromString(hash, document);
}
catch (e) { }
});
window.addEventListener('load', function () {
if (hashTarget === null) {
return;
}
var end = performance.now();
if (end - start > 500) {
return;
}
window.scroll({ top: 0, left: 0 });
assert(hashTarget !== null, 'Hash target should be set on DOM loaded.');
scrollToTarget(hashTarget);
});
}
......
This diff is collapsed.
......@@ -207,15 +207,20 @@
var hashTarget = null;
var start = performance.now();
document.addEventListener('DOMContentLoaded', function () {
hashTarget = HashTarget.fromString(hash, document);
try {
hashTarget = HashTarget.fromString(hash, document);
}
catch (e) { }
});
window.addEventListener('load', function () {
if (hashTarget === null) {
return;
}
var end = performance.now();
if (end - start > 500) {
return;
}
window.scroll({ top: 0, left: 0 });
assert(hashTarget !== null, 'Hash target should be set on DOM loaded.');
scrollToTarget(hashTarget);
});
}
......
This diff is collapsed.
......@@ -21,7 +21,9 @@ export function initializeOnLoadScroll(): void
const start = performance.now();
document.addEventListener('DOMContentLoaded', () => {
hashTarget = HashTarget.fromString(hash, document);
try {
hashTarget = HashTarget.fromString(hash, document);
} catch (e) {} // when URL contains hash which has no corresponding DOM element, just ignore it
});
/**
......@@ -29,6 +31,10 @@ export function initializeOnLoadScroll(): void
* all styles are loaded and offsets are computed correctly - so the scroll will be computed correctly.
*/
window.addEventListener('load', () => {
if (hashTarget === null) { // if target does not exist
return;
}
const end = performance.now();
// If difference between start and stop is greater than 500ms, do nothing.
......@@ -40,7 +46,6 @@ export function initializeOnLoadScroll(): void
window.scroll({top: 0, left: 0});
// Then, scroll down to it smoothly.
assert(hashTarget !== null, 'Hash target should be set on DOM loaded.');
scrollToTarget(hashTarget);
});
}
......@@ -22,6 +22,10 @@
<strong>Load this page with <code>#test</code> in URL. This section should be skipped on the page load.</strong>
</p>
<p>
<strong>Load this page with <code>#testnotarget</code> in URL. There should be no error in console.</strong>
</p>
<!-- placeholder text so that custom easing is visible -->
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam ornare wisi eu metus. Pellentesque ipsum. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Donec ipsum massa, ullamcorper in, auctor et, scelerisque sed, est. Mauris tincidunt sem sed arcu. Curabitur ligula sapien, pulvinar a vestibulum quis, facilisis vel sapien. Sed ac dolor sit amet purus malesuada congue. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris dolor felis, sagittis at, luctus sed, aliquam non, tellus. Proin pede metus, vulputate nec, fermentum fringilla, vehicula vitae, justo. Etiam posuere lacus quis dolor. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat. Integer tempor. Morbi scelerisque luctus velit. Sed vel lectus. Donec odio tempus molestie, porttitor ut, iaculis quis, sem. Suspendisse sagittis ultrices augue.</p>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam ornare wisi eu metus. Pellentesque ipsum. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Donec ipsum massa, ullamcorper in, auctor et, scelerisque sed, est. Mauris tincidunt sem sed arcu. Curabitur ligula sapien, pulvinar a vestibulum quis, facilisis vel sapien. Sed ac dolor sit amet purus malesuada congue. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris dolor felis, sagittis at, luctus sed, aliquam non, tellus. Proin pede metus, vulputate nec, fermentum fringilla, vehicula vitae, justo. Etiam posuere lacus quis dolor. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat. Integer tempor. Morbi scelerisque luctus velit. Sed vel lectus. Donec odio tempus molestie, porttitor ut, iaculis quis, sem. Suspendisse sagittis ultrices augue.</p>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment