Skip to content

Instantly share code, notes, and snippets.

@franzese
Created June 29, 2016 01:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save franzese/9e7c1ab6d3c2a11474a73f5eb0d3d153 to your computer and use it in GitHub Desktop.
Save franzese/9e7c1ab6d3c2a11474a73f5eb0d3d153 to your computer and use it in GitHub Desktop.
Vanilla JavaScript Document Ready
Document.prototype.ready = function(callback) {
if(callback && typeof callback === 'function') {
document.addEventListener("DOMContentLoaded", function() {
if(document.readyState === "interactive" || document.readyState === "complete") {
return callback();
}
});
}
};
// usage
document.ready(function() { alert('ok!'); });
@franzese
Copy link
Author

Tested in Chrome 50+ and Firefox 45+

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