0

I have the following code below. If I scroll all the way to the right and then reload the page, I expected (based on my javascript code) for the window to scroll all the way to the left. However this does not happen. Please can someone advise?

 <html>

<head>
    <meta charset="utf-8">
</head>
<style>

#_1{
border: 1px solid blue;
width:1500px;
height: 400px;
}
</style>

<body>
    <div id="_1"></div>

</body>

<script>
window.onload = function(){

window.scrollTo(0,0);

}

</script>

dave
  • 475
  • 6
  • 17
  • Possible duplicate of [**Force page scroll position to top at page refresh in HTML**](https://stackoverflow.com/questions/3664381/force-page-scroll-position-to-top-at-page-refresh-in-html) – cнŝdk May 22 '17 at 14:05
  • It is not a duplicate because if you run the code for above it does not work – dave May 22 '17 at 14:10
  • It's really strange, it shows the same problem as yours, any way you check [this answer](https://stackoverflow.com/questions/11486527/reload-browser-does-not-reset-page-to-top/11486546#11486546) it gives a solution. – cнŝdk May 22 '17 at 14:13
  • If their code doesn't work for you, why would accept the answer with the same code? – cнŝdk May 22 '17 at 14:18
  • @dave Please once you just visit here hope you find the solution [CLICK](https://stackoverflow.com/questions/1144805/scroll-to-the-top-of-the-page-using-javascript-jquery?rq=1) – Jaykumar Gondaliya May 23 '17 at 05:39

1 Answers1

1

I think you may try this?

window.onbeforeunload = function () {
  window.scrollTo(0, 0);
}

Or

$(document).ready(function(){
    $(this).scrollTop(0);
});

Hope that you like it :)

  • These are the same solutions provided at [**Force page scroll position to top at page refresh in HTML**](https://stackoverflow.com/questions/3664381/force-page-scroll-position-to-top-at-page-refresh-in-html) wich is just an identical duplicate of this answer. – cнŝdk May 22 '17 at 14:08
  • its which not wich bro :) – Jaykumar Gondaliya May 23 '17 at 05:24