Paul Kendal Paul Kendal - 2 years ago 187
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.

Answer Source

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');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download