Paul Kendal Paul Kendal - 5 months ago 34x
Javascript Question

.scroll() to top does not work with .location.reload();

I am trying to use the jQuery

to refresh the page and then
to scroll to the top of the page. Both functions work when implemented alone.

However, when I try to implement both together, the page does not scroll to the top. It initially appears to scroll to the top but is then dragged back to the bottom of the page.

function cancelUpdate(){
$(".cancel-update" ).click(function(event) {
$('html, body').animate({ scrollTop: 0 }, 'fast');
setTimeout(function () {
// $('html, body').animate({ scrollTop: 0 }, 'fast');
}, 5500);

I tried setting the scroll to top within a
function- just in case jQuery needed time to execute the function, but it was to no avail.


There are 2 ways, the first one is to scroll your page first before reloading. The second one you could try to check whether the scroll is on top of the page or not after reloading, if not then scroll to top.

Second way:

    if($('html').scrollTop() != 0)
       $('html, body').animate({ scrollTop: 0 }, 'fast');