Last active
September 14, 2021 20:27
-
-
Save tjbenton/4186f003329c623e53f5d4a31744b054 to your computer and use it in GitHub Desktop.
document.ready Promise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!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
what does this buy us? Is it for compatibility with frameworks or is there a perceived/actual performance improvement on page load?