Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

97
Views
I can't refresh page to top using JavaScript

I used this code but it didn't work, I need some help about how to refresh page to top when it loads.

window.addEventListener('load', function(){
   window.scrollTo(0,0)
})

window.onload = (event) => {
   window.scrollTo(0,0)
};
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Your code works if the page is loading not at top

For example on a back button or if the page is loaded with an ID in the hash

You CAN just remove the onload and have the script inline at the bottom of the page

window.addEventListener('load', function(){
   window.scrollTo(0,0)
})
div { height: 500px; background-color: yellow; }
<div>Div 1</div>
<div>Div</div>
<div>Div</div>
<div>Div</div>
<div>Div</div>
<div>Div</div>
<div>Div</div>
<div>Div</div>
<div>Div</div>
<div>Div</div>
<div>Div</div>
<div>Div</div>
<div>Div</div>
<div>Div</div>
<div>Div</div>
<div>Div</div>
<div>Div</div>
<div id="x">Last Div</div>
<script>
 // mocking the page loading not at top, this is not part of the answer
 document.getElementById("x").scrollIntoView(); //while loading
 
 // put the window.scrollTo(0,0) here if you have issues
 
</script>

about 4 years ago · Juan Pablo Isaza Report

0

Instead of using window.scrollTo(0,0) can you try using document.body.scrollTop = document.documentElement.scrollTop = 0;

Your code should become

window.addEventListener('load', function(){
    document.body.scrollTop = document.documentElement.scrollTop = 0;
})

window.onload = (event) => {
   document.body.scrollTop = document.documentElement.scrollTop = 0;
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!