{"id":20514067,"url":"https://github.com/webreflection/ready","last_synced_at":"2025-09-15T14:28:26.349Z","repository":{"id":65993261,"uuid":"124507491","full_name":"WebReflection/ready","owner":"WebReflection","description":"A pattern to bootstrap any JavaScript on DOMContentLoaded","archived":false,"fork":false,"pushed_at":"2018-03-09T09:00:26.000Z","size":3,"stargazers_count":19,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-16T09:42:25.882Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/WebReflection.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2018-03-09T07:56:35.000Z","updated_at":"2024-09-02T12:58:49.000Z","dependencies_parsed_at":null,"dependency_job_id":"e31f57be-ed90-4156-a7ed-49efc7391832","html_url":"https://github.com/WebReflection/ready","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fready","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fready/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fready/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fready/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/WebReflection","download_url":"https://codeload.github.com/WebReflection/ready/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242117719,"owners_count":20074438,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-15T21:14:37.024Z","updated_at":"2025-03-05T23:15:18.107Z","avatar_url":"https://github.com/WebReflection.png","language":"HTML","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ready\nA pattern to bootstrap any JavaScript on DOMContentLoaded.\n\n[Live test](https://webreflection.github.io/ready/).\n\n\n## the pattern\n```html\n\u003c!-- add these 2 script on top of every page * --\u003e\n\u003c!-- * these MUST be two separate scripts! --\u003e\n\u003cscript\u003ethis.Promise||document.write('\u003cscript src=\"//cdn.jsdelivr.net/npm/es6-promise/dist/es6-promise.auto.min.js\"\u003e\u003c\\x2fscript\u003e')\u003c/script\u003e\n\u003cscript\u003ethis.ready=new Promise(function($){document.addEventListener('DOMContentLoaded',$,{once:true})})\u003c/script\u003e\n\u003c!-- so that every other script after can simply use ready.then(...) to bootstrap --\u003e\n\u003cscript\u003e\n// proof of concept @ https://webreflection.github.io/ready/\nready.then(function () {\n  document.body.innerHTML = 'Yeah, the \u003ca href=\"https://github.com/WebReflection/ready\"\u003eready\u003c/a\u003e pattern works!';\n});\n\u003c/script\u003e\n```\n\n\n## in a nutshell\n\n  * all browsers with native [Promise](https://caniuse.com/#feat=promises) support won't ever use `document.write`\n  * all browser that don't have native `Promise` won't ever have issues with `document.write`\n  * the two different `\u003cscript\u003e` tags [grant the correct execution order](http://webreflection.blogspot.com/2009/12/documentwriteshenanigans.html)\n  * 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](https://github.com/thinkpixellab/PxLoader/issues/5)\n  * 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.\n  * every browser can now use a single global `ready` promise to bootstrap\n\n\n## why\n\nIn September 2015 there was an attempt to [bring `document.ready`](https://github.com/whatwg/html/issues/127) to every browser.\n\nPhilosophy took over, so that in October 2016 there was a follow up\nattempt to [bring `document.{parsed,contentLoaded,loaded} promises`](https://github.com/whatwg/html/pull/1936) to every browsers.\n\nThat resulted into pretty much **nothing for 3 years**, that's why.\n\n\n## improvements ?\n\n  * if you are targeting modern browsers you can fully remove the first script.\n  * if in 2039 they'll ship a global `ready` you can refactor your code via `grep ready.then`\n  * shortcut targeting modern browsers only (Edge, Chrome, Safari, Firefox, others)\n\n```html\n\u003cscript\u003ethis.ready=new Promise($=\u003eaddEventListener('DOMContentLoaded',$,{once:!0}))\u003c/script\u003e\n```\n\nYou read that correctly, the `DOMContentLoaded` event reaches the global `window` too.\n\n\n### license ?\n\nYou can do whatever you want with this pattern, I haven't invented anything.\n\nHowever, be sure the used Promise polyfill license is compatible with your project.\n\nThe proposed [es6-promise](https://github.com/stefanpenner/es6-promise) one uses a MIT License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebreflection%2Fready","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebreflection%2Fready","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebreflection%2Fready/lists"}