JQuery preventDefault() but still add the fragment path to the URL without navigating to the fragment

Posted by jdln on Stack Overflow See other posts from Stack Overflow or by jdln
Published on 2012-03-20T17:22:43Z Indexed on 2012/03/20 17:29 UTC
Read the original article Hit count: 488

My question is similar to this one but none of the answers solve my problem: Use JQuery preventDefault(), but still add the path to the URL

When a user clicks a fragment link, I need to remove the default behaviour of jumping to the fragment but still add the fragment to the URL. This code (taken from the link) will fire the animation, and then add the fragment to the URL. However the fragment is then navigated to, which im my case breaks my site.

$("#login_link").click(function (e) {
    e.preventDefault();
    $("#login").animate({ 'margin-top': 0 }, 600, 'linear', function(){  window.location.hash =     $(this).attr('href'); });

});

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about fragment-identifier