Skip to content

Instantly share code, notes, and snippets.

@gorillamoe
Forked from tjbenton/ready.html
Created January 28, 2019 10:21
Show Gist options
  • Save gorillamoe/3f0e09c38162788a3a43ccc7fbd0ebf0 to your computer and use it in GitHub Desktop.
Save gorillamoe/3f0e09c38162788a3a43ccc7fbd0ebf0 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');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment