{"id":136,"date":"2025-09-04T17:35:54","date_gmt":"2025-09-04T17:35:54","guid":{"rendered":"http:\/\/www.bacondrivencoding.com\/2008\/?p=136"},"modified":"2025-09-04T17:36:39","modified_gmt":"2025-09-04T17:36:39","slug":"ready-for-the-new-era","status":"publish","type":"post","link":"https:\/\/www.bacondrivencoding.com\/2008\/2025\/09\/ready-for-the-new-era\/","title":{"rendered":"Ready() for the new era"},"content":{"rendered":"<p>Back in the day when jQuery ruled the roost, we would use the <tt>ready<\/tt> handler to make sure our code ran after the DOM and stylesheets had completely loaded.<\/p>\n<pre>\r\n<code>\r\n$( document ).ready(function() {\r\n  \/\/ Handler for .ready() called.\r\n});\r\n<\/code>\r\n<\/pre>\n<p>(Or, more likely, the recommended version.)<\/p>\n<pre>\r\n<code>\r\n$(function() {\r\n  \/\/ Handler for .ready() called.\r\n});\r\n<\/code>\r\n<\/pre>\n<p>But of course we don&#8217;t rely on loading jQuery in our <tt>head<\/tt> anymore. But nobody wants to write out the full logic manually.<\/p>\n<pre>\r\n<code>\r\nif (document.readyState != \"loading\") {\r\n  bootstrap();\r\n} else {\r\n  document.addEventListener(\"DOMContentLoaded\", bootstrap, false);\r\n}\r\n<\/code>\r\n<\/pre>\n<p>But if we&#8217;re loading our code asynchronously, we kind of have to. Some people went as far as to synchronously load their JavaScript right before the <tt>&lt;\/body&gt;<\/tt> tag. That will make sure everything is ready when it runs, but at the cost of loading all your JavaScript later.<\/p>\n<p>But there is a nice, short boilerplate that you can use that makes it easy to trigger on ready:<\/p>\n<pre>\r\n<code>\r\nconst ready = new Promise((resolve, _) => {\r\n    if (document.readyState !== 'loading') resolve();\r\n    else document.addEventListener('DOMContentLoaded', resolve);\r\n});\r\n<\/code>\r\n<\/pre>\n<p>Slap that basically anywhere and you can then run <tt>ready.then( ... )<\/tt> anywhere in your code base that you can see it. And you don&#8217;t need to be careful about making sure there&#8217;s only one declaration. (Unless you have dependencies on initialization order.) A short handful of lines to give you a more fluent syntax for scheduling things on ready.<\/p>\n<p>And unlike the old jQuery version, you don&#8217;t need to load it synchronously before everything else.<\/p>\n<link rel=\"stylesheet\" href=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/highlight.js\/11.11.1\/styles\/default.min.css\">\n<script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/highlight.js\/11.11.1\/highlight.min.js\"><\/script><br \/>\n<script>hljs.highlightAll();<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Back in the day when jQuery ruled the roost, we would use the ready handler to make sure our code ran after the DOM and stylesheets had completely loaded. $( document ).ready(function() { \/\/ Handler for .ready() called. }); (Or, more likely, the recommended version.) $(function() { \/\/ Handler for .ready() called. }); But of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-136","post","type-post","status-publish","format-standard","hentry","category-code"],"_links":{"self":[{"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/posts\/136","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/comments?post=136"}],"version-history":[{"count":7,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/posts\/136\/revisions"}],"predecessor-version":[{"id":143,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/posts\/136\/revisions\/143"}],"wp:attachment":[{"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/media?parent=136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/categories?post=136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/tags?post=136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}