Go to the binding after window.location.reload

I am trying to refresh a hash and then reload the page.

$('a[name="' + fragment + '"]').remove(); //don't jump before window reloads window.location.hash = fragment; window.location.reload(true); 

After rebooting, the window does not go to the anchor tag. How can i fix this?

+7
source share
3 answers

This is pretty trivial to achieve in jQuery if you reload the page. Just check the window.location.hash property when loading the page.

 $(document).ready( function( ) { if( window.location.hash ) { // just in case there is no hash $(document.body).animate({ 'scrollTop': $( window.location.hash ).offset().top }, 2000); } }); 

The only caveat is that your hash matches the id of the element you are scrolling.

Demo here

+2
source

MOZILLA DEVELOPER NETWORK suggests using replace :

 function reloadPageWithHash() { var initialPage = window.location.pathname; window.location.replace('http://example.com/#' + initialPage); } 
+1
source

@Tim S. My intention is absolutely not to steal your thunder, but your very low key comment above is, in my opinion, the best solution to this issue, as it is simple, clean and successfully tested in many browsers. Therefore, I create an answer on your behalf:

 window.location.href = url#anchor 

(where url can be current page or other as desired)

0
source

All Articles