How to scroll to top of page with JavaScript/jQuery?
You almost got it - you need to set the scrollTop
on body
, not window
:
$(function() {
$('body').scrollTop(0);
});
EDIT:
Maybe you can add a blank anchor to the top of the page:
$(function() {
$('<a name="top"/>').insertBefore($('body').children().eq(0));
window.location.hash = 'top';
});
Cross-browser, pure JavaScript solution:
document.body.scrollTop = document.documentElement.scrollTop = 0;