shama / on-load

On load/unload events for DOM elements using a MutationObserver
113 stars 19 forks source link

body undefined at observer.observe(...) time #14

Closed Qard closed 6 years ago

Qard commented 8 years ago

I'm using on-load through choo and am getting "Uncaught TypeError: Failed to execute 'observe' on 'MutationObserver': parameter 1 is not of type 'Node'."

It would seem document.body does not existing yet when observer.observe(...) tries to run. If I wrap the observe call in a window.onload = () { ... } it works fine.

shama commented 8 years ago

When would one need to use this before the document has loaded?

Qard commented 8 years ago

My use is with rollup. To get the tree-shaking goodness of rollup, modules need to be loaded using import statements, which happen at the top level and thus end up loading on-load before the document is ready.

hdriqi commented 7 years ago

I got this error too, and then i change on-load module:

 if(document.body){
    observer.observe(document.body, {
      childList: true,
      subtree: true,
      attributes: true,
      attributeOldValue: true,
      attributeFilter: [KEY_ATTR]
    })
  }

the error is gone but does it affect the module functionality??

btw, this error happened because using webpack and import statement. I think webpack is trying to run the module before the DOM loaded.

yoshuawuyts commented 7 years ago

You could perhaps rely on https://github.com/bendrucker/document-ready ? Not sure what webpack does, but that way you can at least be sure certain things don't happen on the DOM before others (:

On Tue, Jan 10, 2017 at 3:18 PM Rahmat Albariqi notifications@github.com wrote:

I got this error too, and then i change on-load module:

if(document.body){ observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeOldValue: true, attributeFilter: [KEY_ATTR] }) }

the error is gone but does it affect the module functionality??

btw, this error happened because using webpack and import statement. I think webpack is trying to run the module before the DOM loaded.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/shama/on-load/issues/14#issuecomment-271586358, or mute the thread https://github.com/notifications/unsubscribe-auth/ACWlet2u2Avp3XQNskQAuy4-k8X2414Qks5rQ5MlgaJpZM4J_w9R .

hdriqi commented 7 years ago

@yoshuawuyts thanks a lot dude!