Prevent automatic browser scroll on refresh

JavascriptBrowser

Javascript Problem Overview


If you go to a page a and scroll around then refresh the page will refresh at the spot where you left it. This is great, however this also occurs on pages where there is a anchor location in the url. An example would be if you clicked on a link http://example.com/post/244#comment5 and refreshed the page after looking around you would not be at the anchor and the page jumps around. Is there any way to prevent this with javascript? So that no-matter-what you would always navigate to the anchor.

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

To disable automatic scroll restoration just add this tag to head section.

<script>history.scrollRestoration = "manual"</script>

It's not supported by IE. Browser compatibility.

Solution 3 - Javascript

After number of failures finally I managed to do the trick. anzo is correct here as using beforeunload will make the page jump to top when a user reloads the page or clicks a link. So unload is the clearly way to do this.

$(window).on('unload', function() {
   $(window).scrollTop(0);
});

Javascript way(Thanks ProfNandaa):

window.onunload = function(){ window.scrollTo(0,0); }

EDIT: 16/07/2015

The jump issue is still there with Firefox even with unload event.

Solution 4 - Javascript

This solution is no longer recommended due to changes in browser behavior. See other answers.

Basically, if an anchor is used we bind to the windows scroll event. The idea being that the first scroll event has to belong to the automatic repositioning done by the browser. When this occurs we do our own repositioning and then remove the bound event. This prevents subsequent page scrolls from borking the system.

$(document).ready(function() {
	if (window.location.hash) { 
		//bind to scroll function
		$(document).scroll( function() {
			var hash = window.location.hash
			var hashName = hash.substring(1, hash.length);
			var element;
			
			//if element has this id then scroll to it
			if ($(hash).length != 0) {
				element = $(hash);
			}
			//catch cases of links that use anchor name
			else if ($('a[name="' + hashName + '"]').length != 0)
			{
				//just use the first one in case there are multiples
				element = $('a[name="' + hashName + '"]:first');
			}

			//if we have a target then go to it
			if (element != undefined) {
				window.scrollTo(0, element.position().top);
			}
			//unbind the scroll event
			$(document).unbind("scroll");
		});
	}
	
});

Solution 5 - Javascript

This works for me.

    //Reset scroll top
    
    history.scrollRestoration = "manual"
    
    $(window).on('beforeunload', function(){
          $(window).scrollTop(0);
    });

Solution 6 - Javascript

Here's a a more general approach. Instead of trying to prevent the browser from scrolling (or jumping to the top as it would look like) I just restore the previous position on the page. I.e. I'm recording the current y-offset of the page in localStorage and scroll to this position once the page has loaded.

function storePagePosition() {
  var page_y = window.pageYOffset;
  localStorage.setItem("page_y", page_y);
}


window.addEventListener("scroll", storePagePosition);


var currentPageY;

try {
  currentPageY = localStorage.getItem("page_y");

  if (currentPageY === undefined) {
    localStorage.setItem("page_y") = 0;
  }
  
  window.scrollTo( 0, currentPageY );
} catch (e) {
    // no localStorage available
}

Solution 7 - Javascript

You can just put a # at the end so the page will load at the top.

Works on all browsers, mobile and desktop, because it is so simple.

$(document).ready(function() {
var url = window.location.href;
console.log(url);
if( url.indexOf('#') < 0 ) {
    window.location.replace(url + "#");
} else {
    window.location.replace(url);
}

});

// This loads the page with a # at the end.

Solution 8 - Javascript

this works absolutely fine. Nice and clean javascript

    var objDiv = document.getElementById("chatbox");
if ( window.history.replaceState ) {
  objDiv.scrollTop = objDiv.scrollHeight;

    window.history.replaceState( null, null, window.location.href );
}

Solution 9 - Javascript

You should be able to.

Onload, check if window.location.hash has a value. If it does, grab the element with an id that matches the hash value. Find the position of the element (recursive calls to offsetTop/offsetLeft) and then pass those values into the window.scrollTo(x, y) method.

This should scroll the page to the desired element.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionThomasReggiView Question on Stackoverflow
Solution 1 - JavascriptjosetapadasView Answer on Stackoverflow
Solution 2 - JavascriptiimosView Answer on Stackoverflow
Solution 3 - JavascriptJanaka DombawelaView Answer on Stackoverflow
Solution 4 - JavascriptmrtshermanView Answer on Stackoverflow
Solution 5 - JavascriptAlex Having FunView Answer on Stackoverflow
Solution 6 - JavascriptOliver SchafeldView Answer on Stackoverflow
Solution 7 - JavascriptDautView Answer on Stackoverflow
Solution 8 - JavascriptMagdi T.View Answer on Stackoverflow
Solution 9 - Javascriptnikmd23View Answer on Stackoverflow