I was just wondering whether this snippet of Javascript is going to slow down my site:
$(function(){
var realLink = location.href;
$( "#nav a" ).each(
function( intIndex ){
String.prototype.startsWith = function(str){
return (this.indexOf(str) === 0);
}
var pageLink = $(this).attr("href");
if ( realLink.startsWith(pageLink) )
$(this).parent().addClass("active");
}
);
});
It only loops about 5-7 times, and I don’t have very much Javascript looping experience.
There’s nothing inherently wrong with this snippet, except that you’re constantly creating and assigning a function to
String.prototype.startsWithin a loop. That, of course, is an unnecessary work, and should at least be:I also don’t see a need for
intIndexargument there. It’s not used anywhere in a function.