beaufortfrancois / sandbox

:baby_chick:
MIT License
71 stars 32 forks source link

Give users the bit of JS that delays the JS until load #11

Open danjenkins opened 7 years ago

danjenkins commented 7 years ago

I always start out with the generator and I always have to fill in the onload functions

I'm guessing this would be useful to other people too

Otherwise you just get "can't add listener to null"

danjenkins commented 7 years ago

I'm talking about https://beaufortfrancois.github.io/sandbox/web-bluetooth/generator/

beaufortfrancois commented 7 years ago

What would you add exactly?

danjenkins commented 7 years ago
function ready(fn) {
  if (document.readyState != 'loading'){
    fn();
  } else {
    document.addEventListener('DOMContentLoaded', fn);
  }
}

Based on http://youmightnotneedjquery.com/#ready