prevent jumping to anchor on hashchange event

  anchor, events, javascript, jquery, scroll

I’d like to prevent jumping to an anchor on a hashchange event. I could figure out how to disable this behaviour when clicking on a link. but unfortunately, the same code doesn’t work for hashchange events. here is my code:

$('.parent').on('click', 'a', function(e) {
    e.preventDefault(); // this works perfectly fine
});

$(window).on('hashchange', function(e) {
    e.preventDefault(); // doesn't work
});

Source: Ask Javascript Questions

LEAVE A COMMENT