GithubHelp home page GithubHelp logo

ready's Introduction

ready

A pattern to bootstrap any JavaScript on DOMContentLoaded.

Live test.

the pattern

<!-- add these 2 script on top of every page * -->
<!-- * these MUST be two separate scripts! -->
<script>this.Promise||document.write('<script src="//cdn.jsdelivr.net/npm/es6-promise/dist/es6-promise.auto.min.js"><\x2fscript>')</script>
<script>this.ready=new Promise(function($){document.addEventListener('DOMContentLoaded',$,{once:true})})</script>
<!-- so that every other script after can simply use ready.then(...) to bootstrap -->
<script>
// proof of concept @ https://webreflection.github.io/ready/
ready.then(function () {
  document.body.innerHTML = 'Yeah, the <a href="https://github.com/WebReflection/ready">ready</a> pattern works!';
});
</script>

in a nutshell

  • all browsers with native Promise support won't ever use document.write
  • all browser that don't have native Promise won't ever have issues with document.write
  • the two different <script> tags grant the correct execution order
  • the {once:true} third argument ensures in modern browsers the listener is cleaned up ASAP and in older browsers it grants that there is a third argument
  • the this.ready= is used, instead of a quirky ready=, to ensure if your project does some sort of linting/variables leaking validation it wouldn't bother that script.
  • every browser can now use a single global ready promise to bootstrap

why

In September 2015 there was an attempt to bring document.ready to every browser.

Philosophy took over, so that in October 2016 there was a follow up attempt to bring document.{parsed,contentLoaded,loaded} promises to every browsers.

That resulted into pretty much nothing for 3 years, that's why.

improvements ?

  • if you are targeting modern browsers you can fully remove the first script.
  • if in 2039 they'll ship a global ready you can refactor your code via grep ready.then
  • shortcut targeting modern browsers only (Edge, Chrome, Safari, Firefox, others)
<script>this.ready=new Promise($=>addEventListener('DOMContentLoaded',$,{once:!0}))</script>

You read that correctly, the DOMContentLoaded event reaches the global window too.

license ?

You can do whatever you want with this pattern, I haven't invented anything.

However, be sure the used Promise polyfill license is compatible with your project.

The proposed es6-promise one uses a MIT License.

ready's People

Contributors

webreflection avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.