0

I just do study JavaScript and I want to execute window.scrollTo() function when page is reloaded, but it doesn't work.

window.scrollTo(0, 200)

I know one way that worked:

setTimeout(() => {
   window.scrollTo(0, 200);
},1)

but I don't want to try this. Is there another option? I want to get the scroll activity to happen when page is loaded.

node_modules
  • 4,790
  • 6
  • 21
  • 37
  • Maybe `window.addEventListener('DOMContentLoaded', () => window.scrollTo(0, 200))`? – NNL993 Nov 29 '22 at 12:33
  • Please refer to https://stackoverflow.com/questions/807878/how-to-make-javascript-execute-after-page-load You can execute a scroll event to any of the listeners mentioned as needed. – shaangon Nov 29 '22 at 12:39

1 Answers1

-2
window.document.body.scrollIntoView({block:'start',behavior:'smooth'})
Hkachhia
  • 4,463
  • 6
  • 41
  • 76
Musaib Mushtaq
  • 407
  • 2
  • 8