I'm trying to update hash and then reload page.
$('a[name="' + fragment + '"]').remove(); //don't jump before window reloads
window.location.hash = fragment;
window.location.reload(true);
After reload window doesn't jump to anchor tag. How can I fix it?
This is fairly trivial to achieve in jQuery if you are reloading 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 to.
Demo here
MOZILLA DEVELOPER NETWORK suggest using replace
:
function reloadPageWithHash() {
var initialPage = window.location.pathname;
window.location.replace('http://example.com/#' + initialPage);
}
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