Scroll to top of the page

Scroll the window back to the top.

JavaScriptBrowser · DOM Version 1.0.0 · MIT · .md

scroll-to-top-of-the-page.js
function goToTop() { window.scrollTo(0, 0); }

Usage

Markup

fixture.html
<div style="height: 3000px"></div>

Script

usage.js
window.scrollTo(0, 1500);
goToTop();
window.scrollY; // → 0

Lines marked → were executed in headless Chromium against the markup above, and each result was compared with the value shown. Lines marked e.g. are illustrative.

Know the limits

  • If the page sets scroll-behavior: smooth, the scroll animates. Keep smooth scrolling inside a prefers-reduced-motion: no-preference query.
  • When the jump is a navigation step, also move focus, for example to the main heading, so keyboard and screen-reader users follow.

The function above is the code the usage lines run. It does not validate its arguments; add checks where it receives untrusted input.

Source & maintenance

Based on Scroll to top of the page from the original 1loc collection by Nguyen Huu Phuoc. Copyright (c) 2020 phuoc-ng. MIT licensed.

Documented as a reference with executed usage lines. Original MDX retained as inert source evidence.

Maintained by Oveyon. Source commit: 14b481cf60df. Read the license.

Usage lines executed in headless Chromium against the fixture markup; no external services · reviewed October 28, 2026.

Keep exploring

Type two or more characters. Try “duplicate”, “webhook” or “retry”.