Hei Hei - 7 months ago 27
Javascript Question

Auto scroll top when page loaded using jQuery

I am trying to use jQuery to scroll the page automatically back to the top when the page is loaded.
Here is my code:

<script type="text/javascript">
$(document).ready(function () {

$(window).scrollTop(0);
return false;

});
</script>


However, the code does not work.
I have also tried to replace
$(window)
to
$('html, body')
,
sadly it still does not work.

So could anyone advice on this?
Thanks a lot!

Answer

Try this

<script type="text/javascript">
    $(document).ready(function () {
        window.scrollTo(0,0);
    });
</script>

The parameters 0,0 are the x and y coördinates.

I hope it helps.