shama / on-load

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

Starting observer only after DOM has loaded #18

Closed jakubfiala closed 6 years ago

jakubfiala commented 7 years ago

This PR makes it so the MutationObserver only kicks off if/after the DOM has fully loaded.

Rationale

This prevents issues when document.body isn't present for any reason. One might argue that it is the developer's responsibility to ensure that the code doesn't execute before body exists. I think it's better to account for that in the module itself.

Likely fixes #14

Implementation notes

Using DOMContentLoaded and checking for complete readystate because I'm assuming you don't support <IE10 anyway.

Using a higher-order function for the event handler for purity and the good of all humankind.

Checklist

💥

bcomnes commented 6 years ago

I agree, this has been a problem for choo and nanocomponent in the past.

bcomnes commented 6 years ago

Looks like this was addressed at some point, but I like the code in this PR better, integrating.

jakubfiala commented 6 years ago

sweet, thanks @bcomnes