Skip to main content

mutationobserver

Observe DOM changes via the MutationObserver API.

Import: @rtorcato/browser-common/mutationobserver

๐Ÿ“– MDN: MutationObserver ยท ๐Ÿ“Š caniuse: mutationobserver

Browser supportโ€‹

APIChromeEdgeFirefoxSafariiOS SafariBaseline
MutationObserverโœ… 26+โœ… 12+โœ… 14+โœ… 7+โœ… 7+Widely available

โœ… supported ยท โ— partial ยท โŒ not supported ยท [n] see note. Generated from MDN browser-compat-data and web-features. caniuse: MutationObserver

Exampleโ€‹

import {
observeMutations,
disconnectMutationObserver,
observeMutationOnce,
} from '@rtorcato/browser-common/mutationobserver'

const obs = observeMutations(document.body, (records) => console.log(records))
disconnectMutationObserver(obs)

observeMutationOnce(container, () => console.log('mutated once'))

Exportsโ€‹

  • observeMutations(element, callback, options?) โ€” observes an element, returns the MutationObserver
  • disconnectMutationObserver(observer) โ€” stops an observer
  • observeMutationOnce(element, callback, options?) โ€” observes and auto-disconnects after the first mutation

See the API reference for full signatures.