No offence, but regex won’t improve that code, only make it slower.
indexOf and .length are way faster than regex.
If anything I would make it $(‘html,body’).stop(true).animate( … );
to make sure previous animations of body and html stop immediately and start the new animation right away, to avoid a long queue of scrolling up and down and up and down and …
var id = $(this).attr("href").match(#(\\b[^\\"]*));
But it’s giving me error
Actually i have created following function to scroll to the named anchors, this function is working fine, i am just trying to improve and shorten the code.
$(document).ready(function() {
$('a[href^=#]').click(function(){
var id = $(this).attr("href").substring( $(this).attr("href").lastIndexOf('#'), $(this).attr("href").length);
$('html,body').animate({scrollTop: $(id).offset().top},'slow');
return false;
});
});
Any other suggestions for improvement are also welcome.