Last active
December 16, 2015 06:19
-
-
Save Jabher/5390453 to your computer and use it in GitHub Desktop.
almost-universal ajaxifier
This file contains 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
var link_processor = function () { | |
var link_selector = 'a[href^="/"], a[href^="' + document.location.origin + '"]'; | |
$('body').on('click', link_selector, function () { | |
var target, all_of_type, target_path; | |
target = $(this); | |
if (target.attr('href')) { | |
target_path = target.attr('href').replace(document.location.origin, ''); | |
var chunks = target_path.split('.'); | |
if ((chunks.length == 1) || (chunks.length > 1 && (chunks[chunks.length - 1] == "htm") && (chunks[chunks.length - 1] == "html"))) { | |
if (document.location.hash.replace('#', '') != target_path) { | |
$(link_selector).each(function () { | |
var t = $(this); | |
t.removeClass('active'); | |
$(t.parent()).removeClass('active') | |
}); | |
if (target_path.split('/').length > 3) { | |
all_of_type = 'a[href*="' + target_path + '"]' | |
} else { | |
all_of_type = 'a[href$="' + target_path + '"]' | |
} | |
$(all_of_type).each(function () { | |
var t = $(this); | |
t.addClass('active'); | |
$(t.parent()).addClass('active') | |
}); | |
target.addClass('active'); | |
target.parent().addClass('active'); | |
document.location.href = document.location.origin + '#' + target_path; | |
} | |
} else { | |
return true; | |
} | |
} | |
return false; | |
} | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment