@pirxpilot/nanoraf
Fork of nanohref. Tiny href click handler library.
Usage
var nanohref = require('@pirxpilot/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"
Ignoring links
By default all href links are handled. The event is not handled under the following conditions:
- the click event had
.preventDefault()
called on it - the link has a
data-nanohref-ignore
attribute - the link has a
target="_blank"
attribute withrel="noopener noreferrer"
- a modifier key is enabled (e.g.
ctrl
,alt
,shift
ormeta
) - the link's href starts with protocol handler such as
mailto:
ordat:
- the link points to a different host
- the link has a
download
attribute
target=_blank
if they also have
rel="noopener"
on them. This is needed to properly sandbox web
pages.
API
nanohref(handler(location))
Create a new anchor click handler.