Skip to content

Commit

Permalink
Merge pull request #39 from lukeburns/master
Browse files Browse the repository at this point in the history
observe document.documentElement again
  • Loading branch information
bcomnes authored Aug 10, 2018
2 parents 795a862 7ed46df commit f6bd84a
Showing 1 changed file with 7 additions and 16 deletions.
23 changes: 7 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 1,6 @@
/* global MutationObserver */
var document = require('global/document')
var window = require('global/window')
var assert = require('assert')
var watch = Object.create(null)
var KEY_ID = 'onloadid' (new Date() % 9e6).toString(36)
var KEY_ATTR = 'data-' KEY_ID
Expand All @@ -24,12 23,16 @@ if (window && window.MutationObserver) {
}
})

if (document.readyState === 'complete') startObserving(observer)()
else document.addEventListener('DOMContentLoaded', startObserving(observer))
observer.observe(document.documentElement, {
childList: true,
subtree: true,
attributes: true,
attributeOldValue: true,
attributeFilter: [KEY_ATTR]
})
}

module.exports = function onload (el, on, off, caller) {
assert(document.body, 'on-load: will not work prior to DOMContentLoaded')
on = on || function () {}
off = off || function () {}
el.setAttribute(KEY_ATTR, 'o' INDEX)
Expand All @@ -41,18 44,6 @@ module.exports = function onload (el, on, off, caller) {
module.exports.KEY_ATTR = KEY_ATTR
module.exports.KEY_ID = KEY_ID

function startObserving (obs) {
return function () {
obs.observe(document.body, {
childList: true,
subtree: true,
attributes: true,
attributeOldValue: true,
attributeFilter: [KEY_ATTR]
})
}
}

function turnon (index, el) {
if (watch[index][0] && watch[index][2] === 0) {
watch[index][0](el)
Expand Down

0 comments on commit f6bd84a

Please sign in to comment.