Skip to content

Instantly share code, notes, and snippets.

@tjbenton
Last active September 14, 2021 20:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tjbenton/4186f003329c623e53f5d4a31744b054 to your computer and use it in GitHub Desktop.
Save tjbenton/4186f003329c623e53f5d4a31744b054 to your computer and use it in GitHub Desktop.
document.ready Promise
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="./ready.js"></script>
<script>
document.ready.then(function() {
console.log('READY!');
});
</script>
</head>
<body>
</body>
</html>
;(function(doc, win, add, remove, loaded, load) {
doc.ready = new Promise(function(resolve) {
if (doc.readyState === 'complete') {
resolve();
} else {
function onReady() {
resolve();
doc[remove](loaded, onReady, true);
win[remove](load, onReady, true);
}
doc[add](loaded, onReady, true);
win[add](load, onReady, true);
}
});
})(document, window, 'addEventListener', 'removeEventListener', 'DOMContentLoaded', 'load');
!function(a,b,c,d,e,f){a.ready=new Promise(function(g){function h(){g(),a[d](e,h,!0),b[d](f,h,!0)}'complete'===a.readyState?g():(a[c](e,h,!0),b[c](f,h,!0))})}(document,window,'addEventListener','removeEventListener','DOMContentLoaded','load');
@chazt3n
Copy link

chazt3n commented Jan 28, 2019

what does this buy us? Is it for compatibility with frameworks or is there a perceived/actual performance improvement on page load?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment