Tiny href click handler library.
var nanohref = require('nanohref')
// Handler automatically attached to window.document
nanohref(function (location) {
console.log('new location is', location.pathname)
})
// Create DOM node
var el = document.createElement('a')
el.setAttribute('href', '/my-link')
el.innerText = 'Click me'
document.body.appendChild(el)
// Trigger click
el.click()
// => "new location is /my-link"
By default all href links are handled. The event is not handled under the following conditions:
.preventDefault() called on itdata-nanohref-ignore attributetarget="_blank" attribute with rel="noopener noreferrer"ctrl, alt, shift or meta)mailto: or dat:download attribute:warning: Note that we only handle target=_blank if they also have
rel="noopener noreferrer" on them. This is needed to properly sandbox web
pages. Once noopener becomes
more widespread, we can drop
noreferrer too (currently just 60% of browsers in use support it).
nanohref(handler(location), [rootNode])Create a new anchor click handler. The second argument is for the root node,
which defaults to window.document.