I've noticed that if you are on a page and you have scrolled down a good bit, if you refresh the page, most browsers will jump you back down to your position. Is there any way to prevent this?
I have looked at two options and neither are consistent across Webkit / Firefox.
window.scrollTo(0, 1);
$('html, body').animate({ scrollTop: 0 }, 0);
Any ideas?
You can check a google search result for an example.
$(window). on('popstate', function(event) { return true; }); and then send the user to the page that you want to prevent URL Reload, passing the boolean value returned by this function.
Use the preventDefault() method on the event object to prevent a page refresh on form submit in React, e.g. event. preventDefault() . The preventDefault method prevents the browser from issuing the default action which in the case of a form submission is to refresh the page.
unbind("keydown", disableF5); /* OR jQuery >= 1.7 */ $(document). off("keydown", disableF5); On a side note: This only disables the f5 button on the keyboard. To truly disable refresh you must use a server side script to check for page state changes.
Click the Start button, type “internet options” and select Internet Options in the search results. In the Internet Properties window, click “Custom tab -> Custom level,” then in the Security Settings window, scroll down until you find “Allow META REFRESH.” Disable this option and click OK.
Works for me:
// Scroll top bro
window.onload = function() {
setTimeout (function () {
scrollTo(0,0);
}, 0);
}
On Chrome, even if you force scrollTop to 0 it will jump afterwards after the first scroll event.
You should bind the scroll to this:
$(window).on('beforeunload', function() {
$(window).scrollTop(0);
});
So the browser is tricked to believe that it was on the beginning before the refresh.
I see no reason why this shouldn't work in all browsers, seems to work for me (with only one function for window.onload, use more and there could be problems) ?
window.onload = function() {
scrollTo(0,0);
}
To make it work when back button is clicked aswell, maybe something like this:
<body onunload="">
<script type="text/javascript">
window.onload = function() {
scrollTo(0,0);
}
</script>
//content here
</body>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With