Takazudo
12/22/2010 - 9:41 AM

$.fn.normalizeAnchor changes all anchors' href which referes the id in the page.

$.fn.normalizeAnchor changes all anchors' href which referes the id in the page.

/**
 * $.fn.normalizeAnchor
 * changes anchor's href which referes the id in the page.
 * ex: change <a href="/foobar/#hogehoge">...</a> to <a href="#hogehoge">...</a>
 *     if your location is http://somewhere/foobar/.
 */
$.fn.normalizeAnchor = function(){
	var l = location;
	var pathWoHash = l.protocol + '//' + l.host + l.pathname;
	return this.each(function(){
		var $el = $(this);
		if($el.is(':not(a)')){
			return;
		}
		var href = $el.attr('href');
		if(href.match(pathWoHash)){
			$el.attr('href', href.replace(pathWoHash, ''));
		}
	});
};