1

Suppose there is a page like http://www.example.com/blahblah/#fragment. With direct input to the address bar the browser loads the page and automatically jumps to #fragment anchor.

Is there any way to prevent this behavior with JavaScript without intervention into HTML code? Maybe there is some event that fires just before the browser jumps to the anchor? The case in point is opening the page through direct input, not the jump after clicking a link.

Also I look for a way to prevent jumping at all, not to let the browser jump and then scroll back.

user3047089
  • 103
  • 1
  • 1
  • 6
  • possible duplicate of [Modifying document.location.hash without page scrolling](http://stackoverflow.com/questions/1489624/modifying-document-location-hash-without-page-scrolling) – Shahadat Hossain Khan Feb 10 '15 at 03:24

1 Answers1

0

I think you should follow Modifying document.location.hash without page scrolling

For simple solution you need to do some tricks like - prepend some text to the hash, so it no longer references an existing element!

You can found possible hints from code snippet below that was copied from one of the answer of above link.

$(function(){
//This emulates a click on the correct button on page load
if(document.location.hash){
 $("#buttons li a").removeClass('selected');
 s=$(document.location.hash.replace("btn_","")).addClass('selected').attr("href").replace("javascript:","");
 eval(s);
}

//Click a button to change the hash
$("#buttons li a").click(function(){
        $("#buttons li a").removeClass('selected');
        $(this).addClass('selected');
        document.location.hash="btn_"+$(this).attr("id")
        //return false;
});
});
Community
  • 1
  • 1