Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

100
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda