Created
November 28, 2011 21:06
-
-
Save klederson/1402065 to your computer and use it in GitHub Desktop.
Nice way to dinamicaly identify current url ( or more closer ) into a menu using javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
var currentURL = metaphone(location.href); | |
var minPercent = 11.00; | |
var lastMatch = ''; | |
$('#mymenu > ul > li > a').each(function() { | |
var url = $(this).attr('href'); | |
var compare = metaphone(url); | |
var dist = levenshtein(currentURL,compare); | |
var percent = ( (dist/Math.max(currentURL.length,compare.length)) * 100 ); | |
if(percent < minPercent) { | |
minPercent = percent; | |
lastMatch = $(this); | |
} | |
// alert(url + ' | ' + location.href + ' ['+percent+'] - ' + compare + ' | ' + currentURL); | |
}); | |
$('#mymenu > ul > li').removeClass('current'); | |
lastMatch.parents('li').addClass('current'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment