-
-
Save vordan/cf00521cc58e6c5caca73f62ef277ed8 to your computer and use it in GitHub Desktop.
Vanilla JavaScript Document Ready (Cross-Browser)
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
var domIsReady = (function(domIsReady) { | |
var isBrowserIeOrNot = function() { | |
return (!document.attachEvent || typeof document.attachEvent === "undefined" ? 'not-ie' : 'ie'); | |
} | |
domIsReady = function(callback) { | |
if(callback && typeof callback === 'function'){ | |
if(isBrowserIeOrNot() !== 'ie') { | |
document.addEventListener("DOMContentLoaded", function() { | |
return callback(); | |
}); | |
} else { | |
document.attachEvent("onreadystatechange", function() { | |
if(document.readyState === "complete") { | |
return callback(); | |
} | |
}); | |
} | |
} else { | |
console.error('The callback is not a function!'); | |
} | |
} | |
return domIsReady; | |
})(domIsReady || {}); |
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(document, window, domIsReady, undefined) { | |
domIsReady(function() { | |
console.log('My DOM is ready peeps!'); | |
document.querySelector('#page').style.background = 'blue'; | |
}); | |
})(document, window, domIsReady); |
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
<div id="page"> | |
This is the DOM! | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment