Created
April 7, 2014 02:33
-
-
Save parris/10014082 to your computer and use it in GitHub Desktop.
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
### | |
Listens for clicks on a tags, if relative and the target is not blank it will use pushState | |
otherwise we fall through to normal click handling. Note preventDefault might need to be | |
handled better in older browsers. | |
### | |
hijackRelativeURLs = -> | |
document.addEventListener('click', (e) -> | |
element = e.target | |
if element?.nodeName == 'A' | |
href = element.attributes.href?.textContent | |
target = element.attributes.target?.textContent | |
isAnchor = href.indexOf('#') is 0 | |
isRelative = href.indexOf('http') isnt 0 | |
if target isnt '_blank' and isRelative and not isAnchor | |
history.pushState({}, '', href); | |
e.preventDefault() | |
return false | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment