Tiny Cross-browser DOM ready function in 111 bytes of JavaScript.
Last active
October 22, 2021 21:52
-
-
Save dciccale/4087856 to your computer and use it in GitHub Desktop.
Tiny Cross-browser DOM ready function in 111 bytes of JavaScript
This file contains hidden or 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> | |
<title>Demo</title> | |
<script> | |
var DOMReady = function(a,b,c){b=document,c='addEventListener';b[c]?b[c]('DOMContentLoaded',a):window.attachEvent('onload',a)} | |
DOMReady(function () { | |
alert('The DOM is Ready!'); | |
}); | |
</script> | |
<h1>Demo</h1> | |
<p>Tiny Cross-browser DOM ready function in 111 bytes</p> |
This file contains hidden or 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
/** | |
* @param {function} a The function to execute when the DOM is ready | |
*/ | |
function(a, b, c) { | |
b = document | |
c = 'addEventListener' | |
b[c] ? b[c]('DocumentContentLoaded', a) : window.attachEvent('onload', a) | |
} |
This file contains hidden or 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){b=document,c='addEventListener';b[c]?b[c]('DOMContentLoaded',a):window.attachEvent('onload',a)} |
Great work, i also see this from https://stackoverflow.com/questions/799981/document-ready-equivalent-without-jquery by Peter Mortensen and Timo Huovinen
function start(f){ /in/.test(document.readyState)?setTimeout(start,5,f) : f()}
usage
start(function() {alert("I'm ready"); });
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
window.onload
is not the same asDOMContentLoaded
https://developer.mozilla.org/en-US/docs/Web/Events/DOMContentLoaded