on-load
Version:
On load/unload events for DOM elements using a MutationObserver
48 lines (34 loc) • 1.63 kB
Markdown
for DOM elements using a MutationObserver
```js
const onload = require('on-load')
const div = document.createElement('div')
onload(div, function (el) {
console.log('in the dom')
}, function (el) {
console.log('out of the dom')
})
// Will fire the onload
document.body.appendChild(div)
// ... some time later
// Will fire the onunload
document.body.removeChild(div)
```
Pass a dom `node` to `onload` to have a `onloadFn` function fire when the dom `node` is added to the document `dom` and a `onunloadFn` fire when the dom `node` is removed from the document `dom`. Optionally a `caller` ID can be set to associate the onload/onunload hooks with a particular instance of of a dom `node`. This is commonly used when 'componentizing' dom nodes.
(c) 2023 Kyle Robinson Young. MIT License
[ ]: https://img.shields.io/npm/v/on-load.svg?style=flat-square
[ ]: https://npmjs.org/package/on-load
[ ]: https://img.shields.io/travis/shama/on-load/master.svg?style=flat-square
[ ]: https://travis-ci.org/shama/on-load
[ ]: http://img.shields.io/npm/dm/on-load.svg?style=flat-square
[ ]: https://npmjs.org/package/on-load
[ ]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
[ ]: https://github.com/feross/standard
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Downloads][downloads-image]][downloads-url]
[![js-standard-style][standard-image]][standard-url]
On load/unload events