loading when window and document are undefined

The code handled this situation 99% of the time, but there were a couple checks that needed to be added.