Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html page load at the bottom

I need an HTML page to automatically scroll down when the page loads. So basically loads at the bottom. Can JavaScipt be used?

Please can you help me or lead my in the right direction.

All help is appreciated. thanks

like image 543
Luke Avatar asked Feb 03 '26 03:02

Luke


2 Answers

Try this:

window.scroll(0, document.documentElement.scrollHeight)
like image 147
Ry- Avatar answered Feb 04 '26 16:02

Ry-


Here is a method that worked for me:

Expected outcome:

  • No scroll animation
  • Loads at bottom of page on first load
  • Loads on bottom of page for all refreshes

Code:

<script>
    function scrollToBottom() {
        window.scrollTo(0, document.body.scrollHeight);
    }
    history.scrollRestoration = "manual";
    window.onload = scrollToBottom;
</script>

Why this may work over other methods:

Browsers such as Chrome have a built-in preset to remember where you were on the page, after refreshing. Just a window.onload doesn't work because your browser will automatically scroll you back to where you were before refreshing, AFTER you call a line such as:

window.scrollTo(0, document.body.scrollHeight);

That's why we need to add:

history.scrollRestoration = "manual";

before the window.onload to disable that built-in feature first.

References:

Documentation for window.onload: https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onload

Documentation for window.scrollTo: https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollTo

Documentation for history.scrollRestoration: https://developer.mozilla.org/en-US/docs/Web/API/History/scrollRestoration

like image 25
ObjectJosh Avatar answered Feb 04 '26 15:02

ObjectJosh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!