if (typeof window != 'undefined' && window.document && window.document.readyState == whatever) { // do stuff }