Skip to content

Instantly share code, notes, and snippets.

@parris
Created April 7, 2014 02:33
Show Gist options
  • Save parris/10014082 to your computer and use it in GitHub Desktop.
Save parris/10014082 to your computer and use it in GitHub Desktop.
###
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