Created
December 22, 2010 09:41
-
-
Save Takazudo/751318 to your computer and use it in GitHub Desktop.
$.fn.normalizeAnchor changes all anchors' href which referes the id in the page.
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
/** | |
* $.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, '')); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment