function ready() {
// do your stuff
}
// this is required for the (not so) edge case where your script is loaded after the document has loaded
// https://developer.mozilla.org/en/docs/Web/API/Document/readyState
if (document.readyState !== 'loading') {
ready();
} else {
// the document hasn't finished loading/parsing yet so let's add an event handler
document.addEventListener('DOMContentLoaded', ready);
}