Skip to content

Instantly share code, notes, and snippets.

@smonn
Created May 30, 2012 19:57
Show Gist options
  • Save smonn/2838578 to your computer and use it in GitHub Desktop.
Save smonn/2838578 to your computer and use it in GitHub Desktop.
My DOM Ready function

Usage:

domready(function () {
  // do stuff that requires the DOM
});
/*jslint indent: 2 */
(function (global) {
'use strict';
var domready = function (callback) {
if (typeof callback !== 'function') {
throw new TypeError('callback is not a function');
}
function loaded() {
callback();
}
if (global.addEventListener) {
global.addEventListener('DOMContentLoaded', loaded, false);
} else if (global.attachEvent) {
global.attachEvent('onload', loaded);
} else {
loaded();
}
};
global.domready = domready;
}(this));
@smonn
Copy link
Author

smonn commented May 30, 2012

Usage example:

window.DomReady(function () {
  console.log('DOM is now ready!');
});

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